[Python] bulidin function-type note

Source: Internet
Author: User

Python type

Type (object)

Return the type of an object. The return value is a type object. The isinstance () built-in function is recommended for testing the type of an object.

Type of the returned object. The returned object is of the type. We recommend that you use isinstance () to check the type of an object.

With three arguments, type () functions as a constructor as detailed below.

When three parameters are used, the type () method can be used as a constructor.

Type (name, bases, dict)

Return a New Type object. this is essential a dynamic form of the class statement. the name string is the class name and becomes the _ name _ attribute; the bases tuple itemizes the base classes and becomes the _ bases _ attribute; and the dict dictionary is the namespace containing definitions for class body and becomes the _ dict _ attribute. for example, the following two statements create identical type objects:

Returns a New Type object. In essence, it is a kind of dynamic statement.

The name parameter is the name of the class and the value of the _ name _ attribute.

The bases tuples list the parent class of this class and become the value of the _ bases _ attribute.

Dict includes the definition of the class body and becomes the _ dict _ attribute

The following is a case:

>>> Class X (object ):

... A = 1

...

>>> X = type ('x', (object,), dict (a = 1 ))

#----------------------------------------------------------------------------

That is to say, this type can generate our customized class at runtime.

Try it by yourself:

Example

Use Type to determine whether the object type is the same:

 1 ############# in [8]: A = '1' in [9]: B = type (a) in [10]: isinstance ('3', B) out [10]: truein [11]: isinstance ([], B) out [11]: false use type to dynamically create Class 2 ################ in [12]: pycon = type ('pycon ', (object,), {'age': 20}) in [13]: type (pycon) out [13]: typein [14]: pycon. ageout [14]: 20In [15]: pycon. _ name _ out [15]: 'pycon '3 ################# in [20]: fun = Lambda X: x + 1In [21]: clafun = type ('clafun', (object,), {'add': Fun}) in [22]: type (clafun. add) out [22]: instancemethodin [26]: A = clafun () in [27]:. add (3) --------------------------------------------------------------------------- typeerror traceback (most recent call last) 
  
    in 
   
     () ----> 1. add (3) typeerror: 
    
      () Takes exactly 1 argument (2 given) # --- but the call fails, A self parameter 4 ################### in [29]: Fun = Lambda self, X: X + 1In [30]: clafun = type ('clafun', (object,), {'add': Fun}) in [31]: A = clafun () in [32]:. add (3) out [32]: 4 
    
   
  

in this way, it is useful to dynamically create classes based on conditions.

Related Article

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.