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 use _ _ str__ and _ _ dict__ in python

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how to use _ _ str__ and _ _ dict__ in python, which has a certain reference value. Interested friends can refer to it. I hope you can learn a lot after reading this article.

_ _ str__, _ _ unicode__

After using the version: python 3 +, delete _ _ unicode__ and replace it with _ _ str__

Description:

1. _ _ str__ is called by the print function, which is usually something called return.

This thing should be expressed in the form of a string. If you don't want to use the str () function to convert.

3. When you print a class, the first thing print calls is the _ _ str__ defined in the class

Class A (): def _ _ init__ (self): self.name='lee' self.age=18 self.job='it' def _ _ str__ (self): # rewrite return 'xxx'a=A () print (A) # output print (a) # output xxxprint (str (a)) # output xxxprint (str (A)) # output print (A. output stringing _) # output print (a.output stringing _) # output _ _ dict__

The property search order of an object follows the order of finding the instance object itself, then the class, and then the parent class of the class.

_ _ dict__ is a dictionary, the key is the attribute name, and the value is the attribute value.

The instance has its own _ _ dict__, and its corresponding class also has its own _ _ dict__

But some special objects do not have a _ _ dict__ attribute, which will not be discussed here

Class A (): def _ _ init__ (self): self.name='lee' self.age=18 self.job='it'a=A () print (a. Output _ output _) # output {'name':' lee', 'job':' it', 'age': 18} print (A. output attributes _)' 'all attributes of the output class {' _ init__': '_ _ dict__':,' _ _ weakref__':,'_ _ doc__': None,'_ _ module__':'_ _ main__'}'

When printing the _ _ dict__ attribute of a class, the properties contained in the class are listed, including some built-in properties and class variables of the class and the constructor _ _ init__

When printing the _ _ dict__ property of an instance object, print the key, value of that object

The skill of changing dictionary to object

When we have a dictionary, such as

Bokeyuan= {"b": 1, "o": 2, "k": 3, "e": 4, "y": 5, "u": 6, "a": 7, "n": 8,}

Now that you want to convert it to an object, you usually write:

Class Dict2Obj: def _ init__ (self,bokeyuan): self.b = bokeyuan ['b'] self.o = bokeyuan ['o'] self.k = bokeyuan ['k'] self.e = bokeyuan ['e'] self.y = bokeyuan ['y'] self.u = bokeyuan ['u'] self.a = bokeyuan ['a'] self.n = bokeyuan ['n']

You can now use _ _ dict__

Class DicToObj (): def _ _ init__ (self,bokeyuan): self.__dict__.update (bokeyuan) Thank you for reading this article carefully. I hope the article "how to use _ _ str__ and _ _ dict__ in python" shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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