Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to retrieve data attributes in Python?

I have a question that is puzzling me recently about which is the best way to retrieve attributes from outside.

Let say I have a class:

class Thing:
    def __init__(self, whatever):
        self.whatever = whatever

x = Thing('foo')

Now I know that if I want to retrieve whatever attribute I can do this:

x.whatever

I have the habit (probably because I come from other oo languages) to define methods to retrieve class attributes as needed and use them insted of retrieve them directly, like:

class Thing:
    def __init__(self, whatever):
        self.whatever = whatever

    def getWhatever(self):
        return self.whatever

In my little experience I've found that using this approach make things easier to mantain in the long term because if I edit the structure of data attributes I have to edit only the specific method.

But since I am not really a python veteran I'd love to know if I am doin' it right or if some other approaches are better and more pythonic. Thoughts?

like image 554
gbr Avatar asked Sep 17 '11 12:09

gbr


People also ask

How to retrieve the class attribute of an object in Python?

In other words, when you use an instance object to retrieve the class attribute, Python will first check if the instance itself has an attribute that has been set with the same name. If none, Python will use the class attribute as the fallback.

What are attribute and methods in Python?

Accessing Attributes and Methods in Python Last Updated : 23 Nov, 2020 Attributes of a class are function objects that define corresponding methods of its instances. They are used to implement access controls of the classes.

How do you get the value of an attribute in Python?

Python's getattr. The built-in function getattr(object, name[, default]) returns the value of a named attribute of object, where name must be a string. If object has an attribute with name, then the value of that attribute is returned.

What is data retrieval using Python?

Python is an open-source scripting language and includes various modules and libraries for information extraction and retrieval. In this article, we will be discussing Data Retrieval Using Python and how to get information from APIs that are used to share data between organizations and various companies.


2 Answers

Defining explicit getters and setters is a bad practice in Python. Instead, use properties:

class Thing(object): # New-style class
    def __init__(self, whatever):
        self._whatever = whatever

    @property
    def whatever(self):
        return self._whatever # Insert complicated calculation here

So instead of pre-planning by using get methods, just introduce a property when you actually need advanced behavior, and not any earlier.

like image 180
phihag Avatar answered Nov 08 '22 11:11

phihag


@phihag has the right idea, and mentions in their answer, but to be more explicit about it: The first step is simply to use the attribute directly:

class Thing(object):
    def __init__(self, whatever):
        self.whatever = whatever


t = Thing(12)
assert t.whatever == 12

Later, if you find you need to make the whatever attribute more sophisticated, you can turn it into a property:

class Thing(object):
    def __init__(self, whatever):
        self._whatever = whatever

    @property
    def whatever(self):
        return something_complicated(self._whatever)


t = Thing(12)
assert t.whatever == 12

This way, the calling code doesn't change, and you have a nice clean API to your object.

like image 40
Ned Batchelder Avatar answered Nov 08 '22 11:11

Ned Batchelder