Python Magic Method: __getattr__,__setattr__,__getattribute____python

Source: Internet
Author: User

Reprint: http://blog.csdn.net/fjslovejhl/article/details/40683547


Seldom have time to read books .... Calm down and take a good look at Python. In fact, he really did not have his first thought so simple:

First, let's look at the definitions of the three magic methods above:

(1) __getattr__ (self, item):

When accessing the Item property of an object, if the object does not have the corresponding property, the method will be invoked to handle ... Here to note, if an object called FJS, he has a property: Fjs.name = "Fjs", then when accessing Fjs.name because the current object has this property, then will not invoke the __getattr__ () method, but directly returned the owning Name property


(2) __setattr__ (self, item, value):

When you try to assign a value to an object's item attribute, it is called ...


(3) __getattribute__ (self, item):

This is only available in the new class, and the access to all attributes of the object will be invoked to handle ... It can be understood that before the __getattr__


Well... With these methods you can do a lot of things ... For example, interceptor what ... Dynamic agent of what ... It is convenient to realize ... At least more convenient than using Java to achieve similar functions ....

However, when you need attention, you need to be very careful when rewriting these methods, because it is easy to make circular calls ....


Here's an example of how to intercept all of the feature accesses, log what when you visit: [python] view plain copy #-*-Coding:utf-8-*-class Fjs (object):          def __init__ (self, name): self.name = name def hello (self): print "said by:", Self.name def __getattribute__ (self, item): print "Accessed the feature:" + Item return object.__getattribute__ (self, I TEM) FJS = FJS ("fjs") print Fjs.name Fjs.hello ()

The output of the above code is as follows: [python] view plain copy accessed the attribute: name fjs accessed the attribute: Hello said by: Access attributes: Name Fjs


It is very simple to achieve the function of interception ... And here you know the __getattribute__ method blocks access to properties and methods ... This is called all of the features of the visit. However, note that: __getattribute__ only in the new class can be used ...


Um .... Next, work with __getattr__ and __getattribute__ to implement a non-cut programming: [Python]   View plain copy # -*-  coding: utf-8 -*-   Class fjs (object):       def __ init__ (self, name):           self.name = name           def hello (self):            print  "said by : ", self.name           def fjs (self, name):           if  name == self.name:                print  "Yes"            else:               

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.