Detailed Explanation of Magic Method of Python Type Conversion

  • 2021-09-05 00:15:28
  • OfStack

This article discusses python's common magic tricks of converting a complex object into a simple object or data type, which are 10 points useful in programming.

1. __str__ Method.

Before explaining this method, let's open an jupyter notebook, create a class at will as follows, and use str () method to output an instance of this class to see what is returned:


class BarChart(object):
 def __init__(self, x, y, labels,color):
  self.x = x
  self.y = y
  self.labels = labels
  self.color = color
 def show(self):
  pass
str(BarChart(x=[1,2,3,], y=[10,30,20],labels=['1','2','3']))

Out[1]: ' < main.BarChart object at 0x0000017B5704D5B0 > '

In daily development, in most cases, it is shaped like < main.BarChart object at 0x0000017B5704D5B0 > This output has no effect on us. However, in python, str () method is often used for forced type conversion. We want to convert an object into a string with a certain meaning, which requires magic methods __str__ . __str__ Method is called when the object is passed to the constructor of str; This method accepts a position parameter (self), as shown in the following example:


class BarChart(object):
 def __init__(self, x, y, labels, color):
  self.x = x
  self.y = y
  self.labels = labels
  self.color = color
 def show(self):
  pass
 def __str__(self):
  return ' I am 1 A bar Figure, my color value is: '+self.color
str(BarChart(x=[1,2,3,], y=[10,30,20],labels=['1','2','3'],color='red'))

Out[2]: 'I am an bar graph and my color value is: red'

2. __unicode__ Methods and __bytes__ Method

The strings in python2 are ASCII strings, while Unicode strings are used in python3, and python3 also introduces the bytes (bytestring) type. Different string families have their own magic methods:

python2 is produced __unicode__ Magic method, which is called when the object is passed to the constructor of unicode and accepts a positional parameter (self); Produced in python3 __bytes__ Magic method, which is called when the object is passed to the constructor of bytes and accepts a positional parameter (self);

3. __bool__ Method

In fact, the truth is similar. __bool__ Called when the object is passed to the constructor of bool. However, this method is named differently in python2 and python3:

Named in python2 __nonzero__ Methods; Named in python3 __bool__ Method.

However, the functionality of both is 1, and they both accept 1 position parameter (self) and return 1 bool value, that is True Or False .

4. __int__ , __float__ And __complex__ Method

If an object defines an int type __int__ Method, the int method is called when the object is passed to the constructor of int. Similarly, if an object defines a __float__ Methods and __complex__ Method is also called when each is passed to the constructor of float or complex. In addition, Long type is owned in python2 (and no longer in python3), so there is corresponding in python2 __str__1 Method.


Related articles: