Abstract
Explains the mechanics of attribute access for new-style Python objects:
- how functions become methods
- how descriptors and properties work
- method resolution order for classes
Read The Book
-
Single HTML Page [ zip | tar.gz ]
-
Multiple HTML Pages [ zip | tar.gz ]
Related
This book is part of a series:
-
Python Attributes and Methods [you are here]
Comments
Brilliant. Thank you very much for the clearest exposition of Python's type system I have seen.
Yes, very well explained. It's a plesure to read articles like this one. By the way, I really like cafepy site design =).
How does one start a new discussion ? I want to start a discuss between PHP and Python , which is better ?
Okay i figured it out. Must be tired tonight. :(
Is there a large python discussion board / community out there somewhere?
http://groups.google.com/group/comp.lang.python
This is excellent.
I got particularly intersted at SpecialType. It would be interstesting to have the distinction between 'type(list)' ('list' is given in the library doc as a builtin function) and types.ListType made clear.
It seems that 'list' is, in fact, a class or type name and, when called becomes an instance constructor.
Colin W.
Any 'class or type name' when called returns the instance - in fact that is the standard way of creating an instance in Python - call the class name. This is true for built-in types as well as classes you define. The built-in
listused to be a function but version 2.2 onward it is a type. Interestingly the behavior seems same as before when you use it to convert something to a list. But technically you are instantiating a list object rather than calling a function.Also,
listandtypes.ListTypeare one and the same object. In fact you can check in Python yourself::When you say
type(list), you get the metaclass<type 'type'>. See http://cafepy.com/article/python_types_and_objects/ch03.html (Python Objects Map), which shows bothlist(shown as<type 'list'>) and<type 'type'>.Thank you very much for these guides. They've answered virtually all of my questions regarding objects and types the standard documentation didn't answer - and there were a lot of those.
You've written that when retrieving an attribute from an object (print objectname.attrname) the first step performed by Python is to check whether attrname is a Python-provided attribute for objectname and if it is - return it. How exactly is this step performed? Also, I've looked for a good guide explaining what is found in object and what the dictproxy object is, and have found nothing - could you refer me to such a guide?
Thanks in advance!
Excellent reading about python specificities! I just would say that chapter 2 could be a little more detaild; I had some difficulty reading it. But nothing very serious - all your material is very readable, and you certainly helped me alot. Thanks!
I think there is an error (or maybe something changed?): in example 1.4, you say:
cobj.dict['d'] = "try to force a value" # Forcing value x = cobj.d # Futile: get is called instead
but some line above you say that resolution order proceed first with instance cobj.dict, then in the type cobj.class.dict
But overriding the class value ( d = Desc() ) with an instance value ( d = [HTML_REMOVED] ) the evaluation order changes. So the x = cobj.d will return the string.
I just tried this with Python 2.6.4:
Probably everyone noted this, but was just a fix.
Good article anyway, I love to read about the internals :)
~Aki
Ooops, sorry, forgot about _ set _ . My last comment is wrong, sorry again. ~Aki
Your insight view is usefule for me. Thanks!
In the "who's next class" section, Both B, C are super classes of D, but in the implementation of the do_your_stuff() and find_next_class method D will only invoke either B or C's do_your_stuff not both. Is it just for illustration purpose
Example 1.7 currently does not work as the hidden variable called b does not exist for a first get. To fix it, you need to set b inside init as in
http://docs.python.org/library/functions.html#property
Otherwise, the first get fails. Also, setting seems to just set, and therefore, delete the descriptor from the instance.
Thank you.
Could you rephrase this part :
«Check objectname.dict for attrname, and return if found. If objectname is a class, search its bases too. If it is a class and a descriptor exists in it or its bases, return the descriptor result.»
The way I understand it is that:
getattirubte looks in object.dict and returns whatever it finds there. If it did not find something it looks inside class bases for a descriptor and returns?
I'm not sure whether getattribute looks for a non-datadescriptor inside the dict of bases or it's looking for a descriptor...
Thanks
I'm a college student working on a Python interpreter project, and the section on descriptors ("The Dynamic
__dict__") saved my life! I couldn't find this level of detail anywhere in the documentation. Thank you so much for providing this.There are a couple of points I have noticed by experimentation (with Python 3.2) that you might want to clarify:
A descriptor with either
__set__or__delete__is considered a data descriptor, and can cause an error if the other one is missing. For example:When looking up an attribute, if a non-data descriptor or a non-descriptor is found before a data descriptor in an object's class, it hides any attributes in superclasses. For instance:
Python-provided attributes have descriptors too (although I'm not sure if the implementation actually uses them). These are the slot wrappers and such in
object,type, etc. Also, a user-defined class can override a Python-provided attribute. For example:It has been largely replaced by LASIK surgery. A variant of RK (limbal relaxing incision or arcuate keratotomy may be chosen to correct mild astigmatism. The eye surgeon uses a diamond scalpel to make cuts on the cornea. [HTML_REMOVED]fake oakleys[HTML_REMOVED] realsociology Blog Archive The MET abuse of stop and search what role did it play in sparking the riots
DJ Pon3's brief appearance at a turntable in A Canterlot Wedding Part 2 was featured in several advertisements<>] and other promotional material, such as a computer wallpaper. One of the Flash assets on The Hub's website depicts her, when the Flash file is decompiled and its assets are rearranged, with magenta irises underneath her purplepink glasses,<>] prior to their appearance in the show.
The revision of the Form I601 , Application for Waiver of Grounds of Excludability was recently announced by United States Citizenship and Immigration Services (USCIS). A person who is termed as ineligible to be admitted to the United States can seek a waiver of certain grounds of inadmissibility using this USCIS immigration form. [HTML_REMOVED]billige ray ban solbriller[HTML_REMOVED] Ray Ban Wayfarer Folding The Problem With Counterfeit Sunglasses
Since the company's establishment, DKNY has accomplished global recognition for demonstrating only the highest standards of creativity, and for producing top quality and innovative products across all DKNY ranges including sunglasses..
My fouryearold daughter was more than happy to be the first to sink her foot into the rented skates. For a mere five dollars, Sparkles will also rent a PVC pipe contraption on wheels for children new to the sport. [HTML_REMOVED]oakleyssunglasses-cheap.com/[HTML_REMOVED]