properties

Why does hasattr execute the @property decorator code block

Why does hasattr execute the @property decorator code block Question: In Python when I call the hasattr on a @property decorator the hasattr function actually runs the @property code block. E.g. a class: class GooglePlusUser(object): def __init__(self, master): self.master = master def get_user_id(self): return self.master.google_plus_service.people().get(userId=’me’).execute()[‘id’] @property def profile(self): # this runs with hasattr return self.master.google_plus_service.people().get(userId=’me’).execute() …

Total answers: 3

How to make the elements of a NumPy array property settable?

How to make the elements of a NumPy array property settable? Question: I have a property of a Python object that returns an array. Now, I can set the setter of that property such that the whole array is settable. However, I’m missing how to make the elements by themselves settable through the property. I …

Total answers: 2

Python read-only lists using the property decorator

Python read-only lists using the property decorator Question: Short Version Can I make a read-only list using Python’s property system? Long Version I have created a Python class that has a list as a member. Internally, I would like it to do something every time the list is modified. If this were C++, I would …

Total answers: 6

Python property vs method when no access to attribute is needed?

Python property vs method when no access to attribute is needed? Question: I am reading “Python programming for the absolute beginner” and in there there is this piece of code: @property def mood(self): unhappiness = self.hunger + self.boredom if unhappiness < 5: m = “happy” elif 5 <= unhappiness <= 10: m = “okay” elif …

Total answers: 5

Python class @property: use setter but evade getter?

Python class @property: use setter but evade getter? Question: In python classes, the @property is a nice decorator that avoids using explicit setter and getter functions. However, it comes at a cost of an overhead 2-5 times that of a “classical” class function. In my case, this is quite OK in the case of setting …

Total answers: 4

How does the @property decorator work in Python?

How does the @property decorator work in Python? Question: I would like to understand how the built-in function property works. What confuses me is that property can also be used as a decorator, but it only takes arguments when used as a built-in function and not when used as a decorator. This example is from …

Total answers: 15

What is the right way to put a docstring on Python property?

What is the right way to put a docstring on Python property? Question: Should I make several docstrings, or just one (and where should I put it)? @property def x(self): return 0 @x.setter def x(self, values): pass I see that property() accepts a doc argument. Asked By: Paul Draper || Source Answers: Write the docstring …

Total answers: 1

Python overriding getter without setter

Python overriding getter without setter Question: class human(object): def __init__(self, name=”): self.name = name @property def name(self): return self._name @name.setter def name(self, value): self._name = value class superhuman(human): @property def name(self): return ‘super ‘ + name s = superhuman(‘john’) print s.name # Doesn’t work 🙁 “AttributeError: can’t set attribute” s.name = ‘jack’ print s.name I …

Total answers: 1

When should an attribute be private and made a read-only property?

When should an attribute be private and made a read-only property? Question: I don’t know when an attribute should be private and whether I should use property. I read recently that setters and getters are not pythonic but using the property decorator is OK. But what if I have attribute, that mustn’t be set from …

Total answers: 10