Overrides for built-in methods in Python classes

Source: Internet
Author: User

Some of the built-in methods are often rewritten in the class for the purpose, most commonly such as __setattr__, with built-in properties to see what happens to overrides

First define a test class with the following code

class Base:     def __init__ (self):         Pass  #  View built-in properties

Printing results are as follows

('__class__', <class 'type'>)('__delattr__', <slot Wrapper'__delattr__'Of'Object'Objects>)('__dict__', Mappingproxy ({'__module__':'__main__','__init__': <function base.__init__At 0x0069a0c0>'__getattr__': <function base.__getattr__At 0x0069a108>'__dict__': <attribute'__dict__'Of'Base'Objects>,'__weakref__': <attribute'__weakref__'Of'Base'Objects>,'__doc__': None})) ('__dir__', <method'__dir__'Of'Object'Objects>)('__doc__', None) ('__eq__', <slot Wrapper'__eq__'Of'Object'Objects>)('__format__', <method'__format__'Of'Object'Objects>)('__ge__', <slot Wrapper'__ge__'Of'Object'Objects>)('__getattr__', <function base.__getattr__At 0x0069a108>)('__getattribute__', <slot Wrapper'__getattribute__'Of'Object'Objects>)('__gt__', <slot Wrapper'__gt__'Of'Object'Objects>)('__hash__', <slot Wrapper'__hash__'Of'Object'Objects>)('__init__', <function base.__init__At 0x0069a0c0>)('__init_subclass__', <built-inchMethod__init_subclass__of type Object at 0x00699030>)('__le__', <slot Wrapper'__le__'Of'Object'Objects>)('__lt__', <slot Wrapper'__lt__'Of'Object'Objects>)('__module__','__main__')('__ne__', <slot Wrapper'__ne__'Of'Object'Objects>)('__new__', <built-inchMethod__new__of type Object at 0x1e1fadb8>)('__reduce__', <method'__reduce__'Of'Object'Objects>)('__reduce_ex__', <method'__reduce_ex__'Of'Object'Objects>)('__repr__', <slot Wrapper'__repr__'Of'Object'Objects>)('__setattr__', <slot Wrapper'__setattr__'Of'Object'Objects>)('__sizeof__', <method'__sizeof__'Of'Object'Objects>)('__str__', <slot Wrapper'__str__'Of'Object'Objects>)('__subclasshook__', <built-inchMethod__subclasshook__of type Object at 0x00699030>)('__weakref__', <attribute'__weakref__'Of'Base'objects>)
built-in properties for base

Find __setattr__ and find that he is a slot wrapper (can be understood as a socket wrapper)

' __setattr__ ' ' __setattr__ ' ' Object ' objects>

Rewrite the __setattr__ to print key and value at the time of assignment

def __setattr__ (self, Key, value):     Print (Key,value) self    . __dict__ [Key] = value

Print again and find __setattr__ a common method

('__setattr__', <function base. __setattr__ at 0x0227f108>)

Summary : When overriding a built-in method, the built-in method changes from slot wrapper to normal function,slot wrapper-->function

----------------------------------------------------------------Split Line------------------------------------------------------ ----------

question : If there is a class inheritance, the parent class overrides, does the subclass also inherit?

1 classBase#Parent Class2     def __init__(self):3         Pass4 5     def __setattr__(self, Key, value):6         Print(key, value)7Self.__dict__[Key] =value8 9 classFoo (Base):#sub-classTen     def __init__(self): One         Pass
Subclass Foo Not overridden method

Scenario 1: Subclass Foo does not rewrite __setattr__, the parent class is __setattr__ exactly the same as the built-in properties of the subclass Foo

# Parent class Base ('__setattr__', <function base. __setattr__ at 0x0243e108>)#  sub-class foo('__setattr__') , <function base. __setattr__ at 0x0243e108>)

Case 2, subclass Foo overrides the __setattr__ method, which is different

# Parent class Base ('__setattr__', <function base. __setattr__ at 0x0243e108>)#  sub-class foo('__setattr__') , <function Foo. __setattr__ at 0x022be198>)

, summary : When there is an inheritance relationship, 1: The parent class overriding the built-in method affects all subclasses

2: Subclasses overwrite methods with inheritance attributes, overwriting the same method of the parent class

3: In multiple inheritance, if the parent class overrides the method, it inherits the method written in the previous parent class (refer to my other article on multiple inheritance)

Overrides for built-in methods in Python classes

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.