Python built-in function type () function an interesting usage _python

Source: Internet
Author: User

Today I saw a code of type on the internet and then looked up the document to see that type has three parameters in use.

Http://docs.python.org/2/library/functions.html

Just know that type can detect object types. Then found an interesting usage.

Copy Code code as follows:

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 argument is the name of the class, the second tuple is the parent, and the third is the property.

Haha, I think it's funny, although the first parameter in my function is written by self, but if you write a piece of code that uses this method to define the class, and then pay no attention to the specification of the class name, the code becomes very difficult to read.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.