Complement 8.python Face Object Part.7 (supplementary to class-related functions)

Source: Internet
Author: User

One, Isinstance (OBJ.CLS) & Issubclass (Sub,super)

    1. Isinstance (OBJ.CLS): Used to check whether an object belongs to a class. (Whether an object is generated by a class)

L1 = List ()

Print Isinstance (l1,list)

Output:

True

2.issubclass (Sub,super): Used to determine whether a class is a subclass of another class.

#sub refers to subclasses, and Super refers to the parent class.

Class C1 (object):

Pass

Class C2 (C1):

Pass

Print Issubclass (C2,C1)

Output:

True


Two, __getattribute__ (built-in method)

This built-in approach, which is similar in name to __getattr__, but the conditions for triggering this built-in method are completely different from the __getattr__!

In an object, __getattr__ is triggered when the property or method you are looking for cannot be found.

__GETATTRBUTE__ will trigger this method regardless of whether the method or property can be found! Once this method is executed, if a Attributeerror exception is not triggered internally, then the __getattr__ method will never be triggered.

Here is an example:

class Class1 (object):

def __init__ (self,x):

self.x = X

def __getattr__ (self, item):

Print "Call __getattr__"

Obj1 = Class1 (123)

Print obj1.x

Output:

Call Test

# then call a non-existent property

obj1.asadsdsadasfasf

Output:

The call __getattr__ #__getattr__ method was successfully executed.


class Class1 (object):

def __init__ (self,x):

self.x = X

def __getattr__ (self, item):

Print "Call __getattr__"

def __getattribute__ (self, item):

Print "Call __getattribute__"

Raise Attributeerror ("^-^") # Throws a Attributeerror

Obj1 = Class1 (123)

obj1.x

Obj1.asasasasasas


Output execution Result:

Call __getattribute__

Call __getattr__

Call __getattribute__

Call __getattr__


From the above results, __getattribute__ when thrown a Attributeerror exception, __getattr__ will be triggered!!



This article is from the "Rebirth" blog, make sure to keep this source http://suhaozhi.blog.51cto.com/7272298/1917990

Complement 8.python Face Object Part.7 (supplementary to class-related functions)

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.