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 call, len, str and repr of python

2025-02-21 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 use call, len, str and repr of python". The content in the article 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 use call, len, str and repr of python.

_ _ call__ method

Description:

Make an instance of a class executable.

Case study:

Class Cat:''' cats''def _ _ init__ (self, name): self.name = name self.count = 0def eat (self): print (f' {self.name} is eating!') Def _ call__ (self, * args, * * kwds)-> int:for I in args:self.count + = 1print (f'{self.name} has caught {I}, has caught {self.count} prey') return self.countwhite_cat = Cat ('big white cat') white_cat ('sparrow', 'mouse') out: the big white cat has caught the sparrow, has caught one prey, the big white cat has caught two prey

The effect of white_cat (Sparrow, Mouse) is exactly the same as that of white_cat.__call__ (Sparrow, Mouse). It is actually a convention made by the interpreter that the instance's _ _ call__ method is actually called after the instance ().

If you define a class that has only one binding method (or a method is used very frequently), you can write that method name as _ _ call__, so that it will be much more convenient for later use and can be executed directly with the instance name ().

_ _ len__ method

Description:

When you execute len () on an instance, you actually call its _ _ len__ method.

Case study:

Class Cat:''' cats''def _ _ init__ (self, name): self.name = name self.preys = [] def _ len__ (self): return len (self.preys) def eat (self): print (f' {self.name} is eating!') Def _ _ call__ (self, * args, * * kwargs): self.preys.extend (args) for i in args:print (f'{self.name} caught {I}') print (f'{self.name} caught {len (self)} prey!) White_cat = Cat ('big white cat') white_cat ('small sparrow', 'small mouse') white_cat ('big sparrow', 'big mouse') white_cat ('kitten fish', 'big herring') out: the big white cat caught the small sparrow, the big white cat caught the small mouse, the big white cat caught two prey! The big white cat caught the big sparrow, the big white cat caught the big mouse, the big white cat caught four prey! The big white cat caught the kitten fish, the big white cat caught the big herring, the big white cat caught six prey!

In this case, the previous code has been optimized, and now the big white cat can still remember all its prey, and when you len (white_cat), you can know how many prey the big white cat has caught.

When you use len () on an instance, you are actually calling the _ _ len__ method of that instance.

_ _ str__ method and _ _ repr__ method

Description:

You can customize the contents of the string when printing the instance.

Case study:

Class Cat:''' cats''def _ _ init__ (self, name): self.name = name self.preys = [] def _ len__ (self): return len (self.preys) def eat (self): print (f' {self.name} is eating!') Def _ _ call__ (self, * args, * * kwargs): self.preys.extend (args) for i in args:print (f'{self.name} caught {I}') print (f'{self.name} caught {len (self)} prey!) Def _ _ repr__ (self): return f' Ah, {self.name} caught the prey "{", ".join (self.preys)}". Def _ _ str__ (self): return f 'wow, {self.name} caught the prey "{", ".join (self.preys)}". White_cat = Cat ('big white cat') white_cat ('little sparrow', 'small mouse') white_cat ('big sparrow', 'big mouse') white_cat ('kitten fish', 'big herring') print (white_cat) out: the big white cat caught the little sparrow, the big white cat caught the little mouse, the big white cat caught two prey! The big white cat caught the big sparrow, the big white cat caught the big mouse, the big white cat caught four prey! The big white cat caught the kitten fish, the big white cat caught the big herring, the big white cat caught six prey! Wow, the big white cat caught the prey of "little sparrow, little mouse, big sparrow, big mouse, kitten fish, big herring".

The _ _ str__ method is called first when the instance is printed, and the _ _ repr__ method is called when there is no _ _ str__ method. When there is neither, it prints something like this: "".

The difference between the _ _ str__ method and the _ _ repr__ method:

The _ _ str__ method is called first when print (), and the _ _ repr__ method is called only when there is no _ _ str__ method.

When you enter the instance enter directly in interactive mode, the content returned by the _ _ repr__ method is displayed.

The difference between the str function and the repr function:

When the parameters are string data types, the contents they return are not exactly the same, and the contents returned by other data types are exactly the same.

Test1 = 123ret1 = str (test1) ret2 = repr (test1) print (ret1, ret2, ret1 = = ret2) test1 = '123'ret1 = str (test1) ret2 = repr (test1) print (ret1, ret2, ret1 = = ret2) out:123 123 True123' 123False

Note: the content obtained by repr the string data will be an extra pair of single quotation marks and the length will be increased by 2; the content obtained by str the string data will be the same as the original.

Thank you for your reading, the above is the content of "how to use python call, len, str, repr". After the study of this article, I believe you have a deeper understanding of how to use call, len, str and repr of python, and the specific use needs to be verified in practice. 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