if updating one property due to an update on another property is what you're looking for [instead of recomputing the value of the downstream property on access] use property setters:
Nội dung chính
- Attributes don't change automatically
- We don't use getter methods
- We prefer properties over getter methods
- How do you change a class attribute in Python?
- How do you access the attributes of a class in Python?
- What is an attribute in Python class?
- How do you initialize a class attribute in Python?
Nội dung chính
- Attributes don't change automatically
- We don't use getter methods
- We prefer properties over getter methods
- How do you change a class attribute in Python?
- How do you access the attributes of a class in Python?
- What is an attribute in Python class?
- How do you initialize a class attribute in Python?
Nội dung chính
- Attributes don't change automatically
- We don't use getter methods
- We prefer properties over getter methods
- How do you change a class attribute in Python?
- How do you access the attributes of a class in Python?
- What is an attribute in Python class?
- How do you initialize a class attribute in Python?
class SomeClass[object]:
def __init__[self, n]:
self.list = range[0, n]
@property
def list[self]:
return self._list
@list.setter
def list[self, val]:
self._list = val
self._listsquare = [x**2 for x in self._list ]
@property
def listsquare[self]:
return self._listsquare
@listsquare.setter
def listsquare[self, val]:
self.list = [int[pow[x, 0.5]] for x in val]
>>> c = SomeClass[5]
>>> c.listsquare
[0, 1, 4, 9, 16]
>>> c.list
[0, 1, 2, 3, 4]
>>> c.list = range[0,6]
>>> c.list
[0, 1, 2, 3, 4, 5]
>>> c.listsquare
[0, 1, 4, 9, 16, 25]
>>> c.listsquare = [x**2 for x in range[0,10]]
>>> c.list
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
2 minute read • Python 3.7—3.10
Watch as video
02:28
Sign in to your Python Morsels account to save your screencast settings.
Don't have an account yet? Sign up here.
Let's talk about how to make an automatically updating attribute in Python.
Attributes don't change automatically
We have a Rectangle
class:
class Rectangle:
def __init__[self, width, height]:
self.width = width
self.height = height
self.area = self.width * self.height
When we call this class, we'll get a Rectangle
object:
>>> rect = Rectangle[3, 4]
This Rectangle
object has a width
, a height
, and an area
:
>>> rect.width
3
>>> rect.height
4
>>> rect.area
12
If we're considering acceptable to change the width
or
height
or an existing Rectangle
object, we might have problem. If we change the width
of a Rectangle
the area
won't change automatically:
>>> rect.width = 10
>>> rect.area
12
This happens because we only assigned the area
one time: in our initializer method. When we first make a Rectangle
object, we set the area
in our __init__
method, and that's it.
We don't use getter methods
In a lot of programming languages, it's common to fix
this problem by making a getter method. Instead of an area
attribute we would make a method [maybe called get_area
or just area
]:
class Rectangle:
def __init__[self, width, height]:
self.width = width
self.height = height
def area[self]:
return self.width * self.height
When we access the area
method now, we'll see that it's a bound method:
>>> rect = Rectangle[3, 4]
>>> rect.area
We can't just access rect.area
to get our area anymore. We have to call the area
method in order to get a result:
Now if we update the width
of our Rectangle
object:
And call the area
method:
Python will recompute the area and give us back the correct result.
In Python, writing getter methods is not common. Instead of using getter method, we tend to make a property.
We prefer properties over getter methods
We're using the property
decorator here:
class Rectangle:
def __init__[self, width, height]:
self.width = width
self.height = height
@property
def area[self]:
return self.width * self.height
That's called a decorator because of that @
syntax.
When we use the property decorator, it will make what was previously a method into a property.
Now if we make a Rectangle
object, just like before, and we access the area
attribute:
>>> rect = Rectangle[3, 4]
>>> rect.area
12
Unlike before, we don't need to put parentheses after area
! In fact, we can't put parentheses. Simply by accessing the area
attribute we get our answer.
So if we change the width
or the height
and we access area
again, it will recompute the area automatically:
>>> rect.width = 10
>>> rect.area
40
So Python isn't actually storing
the area
attribute anywhere. Instead, every time the area
attribute is accessed, it calls area
method and gives us back whatever the return value is. And it does this automatically, simply because we accessed the rect.area
attribute.
Summary
If you want to make an attribute on an object in Python that updates automatically you can use a property.
Series: Properties
We don't use getter and setter methods in Python. Instead we make properties.
Properties allow us to customize what happens when you access an attribute and what happens when you assign to an attribute.
To track your progress on this Python Morsels topic trail, sign in or sign up.
✕
↑
A Python Tip Every Week
Need to fill-in gaps in your Python skills? I send weekly emails designed to do just that.
How do you change a class attribute in Python?
But be careful, if you want to change a class attribute, you have to do it with the notation ClassName. AttributeName. Otherwise, you will create a new instance variable.
How do you access the attributes of a class in Python?
Accessing the attributes of a class getattr[] − A python method used to access the attribute of a class. hasattr[] − A python method used to verify the presence of an attribute in a class. setattr[] − A python method used to set an additional attribute in a class.
What is an attribute in Python class?
Python class attributes are variables of a class that are shared between all of its instances. They differ from instance attributes in that instance attributes are owned by one specific instance of the class only, and are not shared between instances.
How do you initialize a class attribute in Python?
Since Python will automatically call the __init__[] method immediately after creating a new object, you can use the __init__[] method to initialize the object's attributes. The following defines the Person class with the __init__[] method: class Person: def __init__[self, name, age]: self.name = name self.