Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report