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 are the properties of the class and instance of python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces "what are the properties of the class and instance of python". In the daily operation, I believe that many people have doubts about the properties of the class and instance of python. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "what are the properties of the class and instance of python?" Next, please follow the editor to study!

Class namespace and instance namespace

When a class is created, its namespace is created to store all the names defined in the class, which are called attributes. There are two kinds of attributes: data and code blocks (also known as functions or methods).

When an instance is created, its namespace is created to hold one-way class pointers and properties of the instance. An instance can access the properties of a class through a class pointer, while a class cannot access the properties of an instance. In addition, python supports multiple inheritance, that is, there can be multiple class pointers.

Access the instance. Property, if it cannot be found in its namespace, the interpreter will look it up in the namespace of the class (parent class, grandfather class, etc.) through the class pointer, and report an error if none of them are found.

When you modify the property of an instance, if it has this property in its namespace, modify it. If the instance does not have this attribute but has it in the class (parent class, grandfather class, and so on), the attribute is created in the namespace of the instance and the property of the class (parent class, grandfather class) is not modified.

Class Animal:kind = 'animal' def _ init__ (self, name): self.name = namedef eat (self): print (f'{self.name} is eating!') White_cat = Animal ('white cat') white_cat.kind = 'cat' print (white_cat.kind) print (Animal.kind) out: cat

As you can see from the above case, if you modify the kind property of the instance white_cat, the kind property of the Animal will not change.

About self

Self is essentially the address of the instance in memory, which is automatically passed as the first parameter to the _ _ init__ function of the class when the instance is created.

Of course, you can also give it another name, but changing the name will make others confused to read the code, so it is not recommended to change the name!

Properties of classes and instances

There are two ways to view the properties of classes and instances:

Dir (class name): lists the attribute names. Note that many of these are properties defined in the object class.

The class name. _ _ dict__: lists the attribute dictionary, where key is the attribute name and value is the attribute value.

Special properties commonly used by classes and instances

Class name. _ _ name of the name__ class

Class name. _ _ doc__ class documentation

The first parent of the class name. _ _ base__ class

Class name. _ _ tuple of all parent classes of the bases__ class

Class name. _ _ dict__ class attribute dictionary

Class name. _ _ module__ the module in which the class definition resides

The class corresponding to instance. _ _ class__ instance

Here is a special example. Instantiate another instance through its special attribute _ _ class__:

Class Animal:''' animals''kind =' animal 'def _ init__ (self, name): self.name = namedef eat (self): print (f' {self.name} is eating!') White_cat = Animal ('white cat') white_cat.kind = 'cat' black_dog = white_cat.__class__ ('black dog') black_dog.kind = 'dog' print (black_dog.kind) black_dog.eat () out: the black dog is eating! At this point, the study on "what are the properties of the classes and instances of python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Internet Technology

Wechat

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

12
Report