fengxia's profilefengxia的共享空间PhotosBlogNetwork Tools Help

Blog


    March 29

    the difference betwee __getattr__ and __getattribute__

    "i find it difficult to understand the difference between the magic

    methods

    __getattr__ and __getattribute__

    and so donot know when to use former or later.

    can someone brief me on it ?

     

    This is better understood with a bit of history.

    earlier Python versions (before 2.2) the only object model available

    was what we now call "classic classes".

    Classic instances hold their attributes in a dictionary, called __dict

    Attribute lookup starts at this instance dictionary; if not found,

    continues in the class, and its parent class, all along the inheritance

    tree. If still not found, __getattr__ (if it exists) is called, and should

    return the attribute value or raise AttributeError. That is, __getattr__

    is called *last*, and *only* when the attribute was not previously found

    in the usual places.

     

    Since Python 2.2, there are "new style classes" available; they inherit

    directly or indirectly from object. A new style instance may not even have

    a __dict An existing __getattribute__ method is tried *first*; it

    should return the attribute value or raise AttributeError. If no custom

    __getattribute__ exists, the default objectgetattribute__ is used. As a

    last resort, if __getattr__ is defined, it is called."

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://fengxiamu.spaces.live.com/blog/cns!D53B5914FF5CC497!149.trak
    Weblogs that reference this entry
    • None