getter-setter

Minus equal operator doesnt call property setter python

Minus equal operator doesnt call property setter python Question: I have my code setup this way: class Test(): def __init__(self): self.offset = [0,0] @property def offset(self): return self._offset @offset.setter def offset(self,offset): print("set") self._offset = offset test = Test() test.offset[1] -= 1 but the setter is being called only once even though I am changing my …

Total answers: 1

problem defining class property in python

problem defining class property in python Question: I’m new to python programming, I’m trying to encapsulate all property checking of a size in the class object below but one of them is not working (checking to see if withdraw is bigger than amount of books inside the shelf): class Shelf: def __init__(self, capacity=12): self.capacity = …

Total answers: 1

Is there a way to += and -= of an attribute call a instance's method?

Is there a way to += and -= of an attribute call a instance's method? Question: Question: Is there a way to (+= and -=) call (insert_knot and remove_knot) in the class bellow? EDIT: It’s not duplicated from this question, cause I’m changing an attribute of the class (summing and subtracting), not the instance by …

Total answers: 1

Property setter triggers infinite recursion, why?

Property setter triggers infinite recursion, why? Question: Using the @property + setter encapsulation, I need a class that raises an exception when someone tries to change the account_number. However, in the __init__ function, I need to set the value. This is the code, it generates recursion. class SingleBankAccount: def __init__(self, account_number, account_balance): self.account_number = account_number …

Total answers: 1

AttributeError when try "getter" in python

AttributeError when try "getter" in python Question: When I learning getter and setter in python I try below codes. So I create getter to private attribute called _age in class Warrior . But getter in not getting value in _age and give AttributeError: ‘Warrior’ object has no attribute ‘age’. Did you mean: ‘_age’. Can someone …

Total answers: 1

How do getters and setters work in Python?

How do getters and setters work in Python? Question: I am learning getters and setters , what I understand is that they are used so that no one could change the object’s attributes directly. In the example class Person: def __init__(self, name, age): self._name = name self._age = age def get_age(self): return self._age def set_age(self, …

Total answers: 5

How to use property setter as a callback

How to use property setter as a callback Question: I’m working with a legacy system for which a Python interface has been added recently. In my code, I get messages containing ASCII strings for attributes to be set in some wrapper classes. I would like to use a dictionary to map “data labels” to property …

Total answers: 2

What are the differences amongst Python's "__get*__" and "_del*__" methods?

What are the differences amongst Python's "__get*__" and "_del*__" methods? Question: I just started learning Python a few months ago, and I’m trying to understand the differences between the different __get*__ methods: __get__ __getattr__ __getattribute__ __getitem___ And their __del*__ equivalents: __del__ __delattr__ __delete__ __delitem__ What are the differences between these? When should I use one …

Total answers: 1

Using @property versus getters and setters

Using @property versus getters and setters Question: What advantages does the @property notation hold over the classic getter+setter? In which specific cases/situations should a programmer choose to use one over the other? With properties: class MyClass(object): @property def my_attr(self): return self._my_attr @my_attr.setter def my_attr(self, value): self._my_attr = value Without properties: class MyClass(object): def get_my_attr(self): return …

Total answers: 13