An interesting use of the Type of function is Python's built in function

  • 2020-04-02 14:35:50
  • OfStack

Today, I saw a piece of code of type on the Internet, and then I checked the document, and I knew the usage of three parameters of type.

(link: http://docs.python.org/2/library/functions.html#type)

Previously, we just knew that type could detect the object type. Then I found an interesting use.


def println(self):
a = 1 + 1 print " %s,%s " % (self.aa, a) A = type( ' A',(),{ ' aa':'print a', ' println': println}) a = A() type(a)
Out[11]: __main__.A a.println()
print a,2

The first parameter is the name of the class, the second tuple is the parent, and the third is the property.

Haha, I think it's funny, even though the first parameter in my function is still self, but if you write a piece of code that defines a class in this way, and then you don't pay attention to the specification of the class name, the code becomes hard to read.


Related articles: