cafepy.com

Python Attributes and Methods

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

Related

This book is part of a series:

  1. Python Types and Objects

  2. Python Attributes and Methods [you are here]

Comments

01 Jun, 2005
Kevin Whitefoot:

Brilliant. Thank you very much for the clearest exposition of Python's type system I have seen.

23 Jul, 2005
Jeronimo Albi:

Yes, very well explained. It's a plesure to read articles like this one. By the way, I really like cafepy site design =).

06 Sep, 2005
Don Bora:

How does one start a new discussion ? I want to start a discuss between PHP and Python , which is better ?

06 Sep, 2005
Don Bora:

Okay i figured it out. Must be tired tonight. :(

17 Jan, 2006
Anonymous*:

Is there a large python discussion board / community out there somewhere?

18 Jan, 2006
Anonymous*:

http://groups.google.com/group/comp.lang.python

22 Feb, 2006
Anonymous*:

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.

22 Feb, 2006
Shalabh Chaturvedi:

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 list used 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, list and types.ListType are one and the same object. In fact you can check in Python yourself::

import types types.ListType is list True

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 both list (shown as <type 'list'>) and <type 'type'>.

08 Dec, 2006
Anonymous*:

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.

06 Oct, 2007
Anonymous*:

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!

19 Jan, 2010
V. Argenta*:

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!

19 Aug, 2010
AkiRoss*:

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:

>>> class Desc(object):
...   def __get__(self, obj, cls=None):
...     return "Desc Get"
>>> class O(object):
...   de = Desc()
>>> o = O()
>>> o.de
'Desc Get' # As expected
>>> o.__dict__['de'] = "A string"
>>> o.de
'A string' # Resolution order changes

Probably everyone noted this, but was just a fix.

Good article anyway, I love to read about the internals :)

~Aki

19 Aug, 2010
AkiRoss*:

Ooops, sorry, forgot about _ _ set _ _. My last comment is wrong, sorry again. ~Aki

Post Comment
Sign In or provide:
Name*
Email*
Not disclosed
Human Test*
Comment*
Markdown formatting
powered by qp