Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the concept of class properties and instance properties in python

Shulou Source: shulou.com Published: 2022-06-01 08:00:18 10月03日 Update

This article mainly introduces what is the concept of class attributes and instance attributes in python. It is very detailed and has certain reference value. Friends who are interested must finish reading it.

Attributes: class properties and instance properties

Class attributes: all the properties of the class object

Summary

'Class properties can be accessed by both class objects and instance objects

Instance properties can only be accessed by instance objects

Class Student: name=' Dawn'# belongs to the class attribute is the def _ _ init__ (self,age) owned by the Student class object: self.age=age # instance property pass pass lm=Student (17) print (lm.name) # access the class property print (lm.age) through the instance object

Class Student: name=' Dawn'# belongs to the class attribute, which is def _ _ init__ (self) owned by the Student class object. Age): self.age=age # instance properties pass pass lm=Student (17) print (lm.name) # access class properties print (lm.age) print through instance objects ('- access name-' through class objects) print (Student.name) # print (Student.age)

Class Student: name=' Dawn'# belongs to the class attribute, which is def _ _ init__ (self) owned by the Student class object. Age): self.age=age # instance properties pass pass lm=Student (17) print (lm.name) # access class properties print (lm.age) print ('- access name-' through class objects) print (Student.name) # such as class names. Access print (Student.age) in the form of an attribute name

How to modify the class attribute class Student: name=' Dawn'# belongs to the class attribute is the def _ _ init__ (self) owned by the Student class object Age): self.age=age # instance property pass passStudent.name=' Zhao Liying'# the class property has been modified at this time lm=Student (17) print (lm.name) # access the class property through the instance object lm.name=' Michelle Reis'# modify the class property through the instance object cannot be modified print (lm.name) # add a new attribute for the following key Use print (lm.age) print ('- floret data -') xh=Student (28) print (xh.name) # name or Dawn print (xh.age) print ('- access name-' through class objects) # print (Student.name) # print (Student.age)

These are all the contents of the article "what are the concepts of class properties and instance properties in python". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Attributes objects instances that is concepts content articles values interests buddies partners summaries florets forms data more Michelle Reis knowledge class names industries Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Technology Huawei Xiaomi Redmi