#

Wednesday, August 27, 2025

Private Memberships

We can have Private Attributes and Private Methods inside a Class which should be accessed from within the Class only. It is just something Python community use, not something enforced. It is just an  These can be seen mostly inside Modules where the authors use the "_" at the beginning of the attribute / method indicating, don't try to call it from outside the Class which may break the functionality.

Here in this example, you can see that "_domains" is a private attribute. There can be private methods as well which starts with "_". 

However you can modify it if you want, when you call it from somewhere else with using "_" Infront of the attribute / method.
 
Property Decorator with Getters & Setters

A decorator in Python is a function that takes another function as its argument, and returns another function. Property Decorator in Python is a built in decorator 

In this example, we can see _name and _id are private attributes. 

As an example, 1st Syntax @property (line 6) and the method under it (line 7-8) is just coded to return the value of "_name" when just "name" is called from outside the class or anywhere in the program. If this method is not created, the value of _name can be outputted only by calling "R1._name". Because of this method, it is possible to call the same with "R1.name()". But to me more Pythonic, we can use Property Decorator (which is what we have done here with @property keyword), we can call just like a natural attribute "R1.name".



This @property is called a "Getter" as it is used to get values.





Setters can be used to set another value to an attribute, based on a condition.

Line 14 starts a "Setter" which sets a new value (line 22) later given in the program to the original value ( line 20) if the condition on line 16 matches else it will pop up the error message.

Saturday, August 16, 2025

Object Oriented Programming (OOP) helps group objects based on classes. These classes consist of Properties (Attributes) and Behaviours (Methods) which are inherited to the objects we create from that classes. So these classes act as blueprints of creating objects.

Instances are nothing but the classes that hold the data and can perform the behaviours.

Following is a code of a class.








Above code is a Class named "Routers" and working_layer is a Class Attribute.
"brand" and "OS" are Instance Attributes.
"description" is a Method, specifically an Instance Method which is the most common method we see.

Following is an example of creating an object.

Since there are 2 Instance attributes defined within the class, we have to pass values for those 2 attributes like arguments.

Here an object named "R1" is created based on the class "Routers" and brand and OS is mentioned when creating the object. Later they were retrieved using <object name>dot operator<attribute>.


Another object is created from the same "Routers" class giving it's Instance Attributes.

Since the "working_layer" is a class attribute, both R1 and R2 shares the same info while the other info is different.




If we call the Method, the out put will be like the following,