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

How to understand the reflection of python object-oriented programming

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

Share

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

This article mainly explains "how to understand the reflection of python object-oriented programming". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand the reflection of python object-oriented programming".

Definition of reflection

Reflection in python is to find the memory address pointed to by a string name in the namespace, and to obtain, add, delete, judge and other operations on the memory address. Reflection is a string-based event-driven! It is very powerful and can be used to simplify code in many scenarios after proficiency.

Getattr

Parameters:

There are two parameters: the first parameter is used to specify the namespace; the second parameter is a name in the format of a string, and an error will be reported if the name does not exist.

Function:

Returns the memory address pointed to by a string-formatted name in the specified namespace (if the address is a string, it can be used as a string, or if it is a code block, it can be called).

Case study:

Class Animal:kind = 'animal' def _ init__ (self, name): self.name = namedef eat (self): print (f'{self.name} is eating!') White_cat = Animal ('white cat') print (getattr (white_cat, 'name')) print (getattr (Animal,' kind')) getattr (white_cat, 'eat') () print (getattr (Animal,' name')) out: White cat white cat is eating! AttributeError: type object 'Animal' has no attribute' name'

Explanation:

In the above case, the Animal class is defined and its instance white_cat is created.

The print (getattr (white_cat, 'name')) statement is equivalent to print (white_cat.name)

The getattr (white_cat, 'eat') () statement is equivalent to white_cat.eat ().

Print (getattr (Animal, 'name')) made a mistake in getting the' name' Times'in the Animal namespace because the name 'name'' is not in the Animal namespace.

Hasattr

Parameters:

There are two parameters: the first parameter is used to specify the namespace; the second parameter is a name in the format of a string.

Function:

Determines whether the name is in the specified namespace. If so, True is returned. If not, False is returned.

Case study:

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

Explanation:

In the above case, the Animal class is defined and its instance white_cat is created.

The print (hasattr (white_cat, 'eat')) statement determines whether the name eat exists in the namespace of the instance white_cat. The result is True, indicating that the name exists.

The print (hasattr (Animal, 'name')) statement determines whether the name name exists in the namespace of class Animal. The result is False, indicating that there is no such name.

Callable

Parameters:

There is one parameter: the parameter is the memory address (which can be pointed to by the name or returned by getattr).

Function:

To determine whether the code block is stored in the memory address, True is returned if it is, and False is returned if not.

Case study:

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

Explanation:

In the above case, the Animal class is defined and its instance white_cat is created.

The print (callable (getattr (white_cat, 'eat')) statement prints to determine whether eat points to a block of code in the namespace of the instance white_cat. The result is True, which means that it is a code block.

The print (callable (getattr (Animal, 'kind')) statement prints to determine whether kind points to a block of code in the namespace of class Animal. The result is False, which means it is not a code block.

Setattr

Parameters:

There are three parameters: the first parameter is the namespace; the second parameter is the name; and the third parameter is the memory address where the code block is located (which can be a name or a getattr return).

Function:

The function adds a block of code with the specified name (the second parameter) to the specified namespace (the first parameter) (the third parameter), and the return value is None.

Case study:

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

Explanation:

Note the following when using this function:

Setattr (Animal, 'work', work) binds the work method to the Animal class so that all instances of Animal can call the work method.

Setattr (white_cat, 'work', work) binds the work method to the instance white_cat, calling with the instance itself as the first parameter, for example: white_cat.work (white_cat).

In addition, after binding the method to the instance, the method only exists in the namespace of the instance, and other instances of the Animal class cannot be called!

Delattr

Parameters:

There are two parameters: the first parameter is used to specify the namespace; the second parameter is a name in the format of a string, and an error will be reported if the name does not exist.

Function:

The function is to find the string-formatted name in the specified namespace, delete it and release the resource if it is found, and report an error if it cannot be found. The return value is None.

Case study:

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

Explanation:

Note that the delattr function only looks for names in the specified namespace and does not look for names in namespaces such as this class, parent class, grandfather class, and so on, in the order in which the class inherits. This is not the same as when calling a property or method of an instance.

When calling the property or method of the instance, if the instance does not have this name in its own namespace, it will look for the name in the namespace of the class, parent class, grandfather class and so on according to the inheritance order of the class.

Thank you for your reading, the above is the content of "how to understand the reflection of python object-oriented programming". After the study of this article, I believe you have a deeper understanding of how to understand the reflection of python object-oriented programming. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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