Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which of these is the best practice for accessing a variable in a class? [closed]

If I have an object, and within that object I've defined a variable, which of these methods would be considered 'best' for accessing the variable?

Method One

Using a getter function

class MyClass:
    def __init__(self):
        self.the_variable = 21 * 2

    def get_the_variable(self):
        return self.the_variable

if __name__ == "__main__"
    a = MyClass()
    print(a.get_the_variable())

Method Two

Using the @property decorator

class MyClass:

    def __init__(self):
        self._the_variable = 21 * 2

    @property
    def the_variable(self):
        return self._the_variable

if __name__ == "__main__"
    a = MyClass()
    print(a.the_variable)

Method Three

Simply accessing it directly

class MyClass:
    def __init__(self):
        self.the_variable = 21 * 2

if __name__ == "__main__"
    a = MyClass()
    print(a.the_variable)

Are any of these methods more pythonic than the others?

like image 365
Khavan Guneratne Avatar asked Dec 18 '18 15:12

Khavan Guneratne


People also ask

Which variable is used to access the class variables and class methods?

Instance methods can access class variables and class methods directly.

Which variables can be accessed only within the class?

Private Access Modifier - PrivateMethods, variables, and constructors that are declared private can only be accessed within the declared class itself.

What type of access should you use if you want the variables and methods of a class to be accessible only to that class and its subclass?

Private access modifier allows the least accessibility with the private data members to be accessible only within the class. Modifiers limit the scope of data members like classes, constructors, methods, and variables and define the limit as to which classes or packages can access them.

Which of the following access modifier can be accessed with a class?

For members, there are two additional access modifiers: private and protected . The private modifier specifies that the member can only be accessed in its own class.


1 Answers

Method 3 is the standard pythonic way to start. If you need additional logic, filtering or some other behavior for the attribute you can always go back and add a method for the attribute and use the @property decorator at a later time. That's the beauty of python, start with something simple that works. If you later need finer control over the attribute you can create the property and not have to update/change any of the client code that uses the attribute. The client code will not know the difference between accessing the attribute directly vs calling a method and as a result does not have to change.

This ideology is confirmed via PEP 549

Python's descriptor protocol guides programmers towards elegant API design. If your class supports a data-like member, and you might someday need to run code when changing the member's value, you're encouraged to simply declare it as a simple data member of the class for now. If in the future you do need to run code, you can change it to a "property", and happily the API doesn't change.

like image 163
Kevin S Avatar answered Nov 13 '22 23:11

Kevin S