Python-built-in functions for classes, instances, and other objects

Source: Internet
Author: User

1. issubclass ()

The issubclass () Boolean function determines whether a class is a subclass or Child class of another class.

issubclass(sub, sup)
If issubclass () returns True: the sub class given is determined to be a subclass of the parent sup (otherwise, False ). This function also allows non-strict subclass, meaning that a class can be considered as its own subclass. The second parameter of issubclass () can be a possible tuple consisting of the parent class. If the first parameter is a subclass of any candidate class in the given tuples, returns True.


2. isinstance ()

The isinstance () Boolean function is useful when determining whether an object is an instance of another given class.

isinstance(obj1, Class)
Isinstance () returns True if obj1 is an instance of Class or an instance of a subclass of Class (otherwise, False ).

>>> class C1:...     pass...>>> class C2:...     pass...>>> c1 = C1()>>> c2 = C2()>>> isinstance(c1, C1)True>>> isinstance(c2, C1)False>>> isinstance(c1, C2)False>>> isinstance(c2, C2)True>>> isinstance(C2, c2)Traceback (most recent call last):  File "
 
  ", line 1, in 
  
   TypeError: isinstance() arg 2 must be a type or tuple of types
  
 
Note: The second parameter should be a class. Otherwise, a TypeError is returned.

Like issubclass (), isinstance () can also use a tuples as the second parameter. If the first parameter is an instance of any candidate type or class of the given tuples In the second parameter, True is returned.


3. hasattr (), getattr (), setattr (), delattr ()

When using these functions, the object being processed is passed in as the first parameter, but the attribute name, that is, the second parameter of these functions, is the string name of these properties.

Hasattr () is used to determine whether an object has a specific attribute. It is generally used to check an Attribute before it is accessed. The getattr () and setattr () functions obtain and assign values to the attributes of an object accordingly. getattr () will trigger an AttributeError exception when you try to read a non-existent attribute, unless the optional default parameter is provided. Setattr () will either add a new attribute or replace an existing attribute. The delattr () function deletes attributes from an object.

>>> class myClass:...     def __init__(self):...             self.foo = 100...>>> myInst = myClass()>>> hasattr(myInst, 'foo')True>>> getattr(myInst, 'foo')100>>> hasattr(myInst, 'bar')False>>> getattr(myInst, 'bar')Traceback (most recent call last):  File "
 
  ", line 1, in 
  
   AttributeError: 'myClass' object has no attribute 'bar'>>> getattr(myInst, 'bar', 'oops!')'oops!'>>> setattr(myInst, 'bar', 'my attr')>>> getattr(myInst, 'bar')'my attr'>>> delattr(myInst, 'foo')>>> hasattr(myInst, 'foo')False
  
 

4. dir ()

Use dir () to list all attributes of a module.

A. dir () is used on the instance to display the instance variables, as well as the methods and class attributes defined in the class where the instance is located and all its base classes.

B. When dir () Acts on the class, the content of the class and all its base classes in _ dict _ is displayed.

When c. dir () is used on a module, the _ dict _ content of the module is displayed.

D. dir () without parameters, the caller's local variables are displayed.

5. super ()

The purpose of this function is to help the programmer find the corresponding parent class and then conveniently call related attributes. Super () returns a super object, which is responsible for method parsing. When a problem is asked about its features, the system searches for all the superclasses (and the superclasses) until the desired features are found.


6. vars ()

Vars () returns a dictionary that contains the attribute (key) and value of the object stored in its _ dict.

>>> class C:...     pass...>>> c = C()>>> c.foo = 100>>> c.bar = 'Python'>>> c.__dict__{'foo': 100, 'bar': 'Python'}>>> vars(c){'foo': 100, 'bar': 'Python'}
If the object is not provided as a vars () parameter, it will display a dictionary containing the attribute (key) and Its Value of the local namespace, that is, locals ().


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.