What are python class attributes

First, let's look at the definition of attributes

Definition of attributes: Attributes in python are actually derived from common methods.

There are three ways to manipulate class attributes:

  1. Use the @property decorator to manipulate class properties.

  2. Use classes or instances to directly manipulate class attributes (for example: obj.name, obj.age=18, del obj.age)

  3. Use python built-in functions to manipulate attributes.

The meaning of attribute existence:

1、 When accessing attributes, you can create the same illusion as accessing fields. Attributes are derived from methods. If there are no attributes in Python, methods can completely replace their functions.

2、 Defining an attribute can dynamically obtain a certain attribute value, and the attribute value is realized by the corresponding method of the attribute, which makes the application more flexible.

3、 You can formulate your own attribute rules to prevent others from modifying the attribute values at will.

The following describes in detail three methods of operating class attributes:

  1. Use the @property decorator to manipulate class properties.

When defining, add the @property decorator on the basis of the ordinary method; the property has only one self parameter, and no parentheses are required when calling;

advantage:

  1. @ The property decorator can implement the functions of getter, setter and deleter that other languages have (for example, to obtain, set, and delete hidden properties)

  2. The @property decorator can control the value and assignment of properties and improve the stability of the code.

Example code 1:

# encoding=utf-8classGoods(): #New style
 @ property
 def price(self): #View attribute value
 print('@property ') 
 @ price.setter #Modify and set attributes
 def price(self, value):print('@price.setter') 
 @ price.deleter#Delete attribute
 def price(self):print('@price.deleter') 
 
obj =Goods(50) 
obj.price#Automatic execution@Property modified price method and get the return value of the method
obj.price =2000 #Automatic [email protected] modified price method, and assign 2000 to the method parameter
del obj.price #Automatic [email protected] modified price method
Result output:
@ property
@ price.setter
@ price.deleter

Example code 2: Use the @property decorator to control the value and assignment of properties

classGoods(object):
 def __init__(self):
 self.value=50
 @ property
 def price(self): #View properties
 return self.value
 
 @ price.setter #Add or set attributes (attribute name.setter)
 def price(self, value):if value  =50 and value<=100: #Control the value and assignment of attributes
  self.value=value
  print(self.value)else:print("Please enter a number between 50 and 100!")
 
 @ price.deleter #Delete attribute (attribute name.deleter) Note: Once the attribute is deleted, it cannot be set and obtained
 def price(self):
 del self.value
 print("price is deleted!")
 
obj =Goods()print(obj.price)  #Automatic execution@Property modified price method and get the return value of the method
obj.price=106   #Automatic [email protected] modified price method, and assign 106 to the method
del obj.price   #Automatic [email protected] modified price method
Result output:
50
Please enter a number between 50 and 100!
price is deleted!

Example extension:

classDog(object): 
 
 name ='dog' 
 
 def init(self): 
 
 self.age =18
 
d1 =Dog()

d2 =Dog()

Here are two examples d1 and d2.

d1.name #Output dogd2.name #Output dogd1.name ='abc'
d1.name #Output abcd2.name #Output dogDog.name #Output dog

The reason is that d1.name outputs dog not because this instance shares the class attribute, but because this instance does not have the dog attribute, so python looks up the class attribute. But once you modify d1.name, which is equivalent to binding the name attribute to the d1 instance, d1.name has nothing to do with the class attribute. In this case, it is not possible to talk about sharing class attributes between instances, because as long as a value is assigned, it is equivalent to binding an attribute. The meaning of d1.name and d2.name above are different, and their values are also different. It is obvious that their data is not shared.

So far, this article on what is the python class attribute is introduced. For more related python class attributes, please search for the previous articles of ZaLou.Cn or continue to browse the related articles below. Hope you will support ZaLou.Cn more in the future. !

Recommended Posts

What are python class attributes
​What are the numbers in Python?
What are the required parameters of python
What are web development frameworks in python
What are the advantages of python language
What can Python do
What conditions are needed for infinite loop in Python
What are the methods for python to connect to mysql
What are the ways to open files in python
What can python crawlers crawl
What databases can python use
What can python collections do
What is introspection in python
What is object-oriented in python
What is Python variable scope
What is list comprehension in python
What does rc1 mean in python
What does def in python do
What is the use of Python
There are several keywords in Python
There are several versions of python
Simple usage of python definition class
Summary of Python calling private attributes
What does np do in python