0
class Investor:
    def __init__(self, profile):
        self.profile = profile

    def __getitem__(self, item):
        return self.profile[item]

It is ok to access Investor profile by simply Investor['name'], But it comes to an error when I use get() Investor.get('name')

Raised: AttributeError: 'Investor' object has no attribute 'get'

I know I can fix it by adding a get() method to Investor Class, but is it a right way to do? or are there any other special method __get__ or whatever?

2
  • 1
    getters and setters! Yes, as far as I know it is a proper way Commented Jul 4, 2018 at 12:54
  • Maybe you should specify why you need a class at all. Nothing in this code suggests that you cannot use a plain dict or inherit form dict without overriding anything. Commented Jul 4, 2018 at 13:03

6 Answers 6

5

The standard get has a default as well. So this would be the full version:

def get(self, item, default=None):
    return self.profile.get(item, default=default)

As for this being proper, as far as I know there isn't any better way so it is by default.

Sign up to request clarification or add additional context in comments.

Comments

3

Why don't you just define a get function?

def get(self, item):
    return self.profile.get(item)

Comments

2

As mentioned, there isn't a special "get" function which already exists and you can inherit from the object class. To get the functionality you want, you need to implement your own "get" function.

If you actually want to create a lot of similar classes to Investor which all have a get() function, then you should create a superclass for Investor to inherit from.

class Person(object):
    def __init__(self, profile):        
        self.profile = profile

    def get(self, item):
        if item in self.profile:
            return self.profile[item]

class Investor(Person):
   def __init__(self, profile):
       super().__init__(profile)

2 Comments

Why duplicate the implementation of dict.get when you can just call self.profile.get(item)?
I'm not. self.profile[item] is calling the getitem function.
0

How about using @property ?

class Investor:
    def __init__(self, profile):
        self._profile = profile

    @property
    def profile(self):
        return self._profile


if __name__ == "__main__":
   inv = Investor(profile="x")
   print(inv.profile)

Gives:

x

Comments

0

The most simple solution that you can have is to use try:#code except: #code block in __getitem__ method.For ex:

class Investor:
    def __init__(self, profile):
       self.profile = profile

    def __getitem__(self, item):
       try:
         return self.profile[item]
       except:
         return 0

`

This will help you to get dictionary get() method like features without having to add new get() method.

5 Comments

I don't think there's any good reason to break the expected behavior of [] like this.
What if profile is a list.Then, I think it is the shortest and efficient way of getting dict get() method like functionality.
If you want get-like behavior, define a get method, like dict does.
But list has no get method, and what if profile is a list.
Why should it be a list? Just because sequences and mappings both use [] for indexing doesn't mean the two fill the same use cases. Nothing in the question suggests the OP wants to support lists.
0

Assuming you have an investor_object, like:
investor_object = Investor({'name': 'Bob', 'age': 21})

You can do either:
investor_object.profile['name']
or
investor_object.profile.get('name')

Gives:
Bob

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.