Question about accessing class-attributes.

Michele Simionato mis6 at pitt.edu
Fri May 2 11:05:34 EDT 2003


"Bjorn Pettersen" <BPettersen at NAREX.com> wrote in message news:<mailman.1051832523.2752.python-list at python.org>...
> <snip>
> Well, let's see if I know Python well enough to explain what it's doing
> or perhaps what it should be doing...
> 
> Given an algorithm for instantiating a class, and creating a class
> object (i.e. at runtime when you see "class X(object): ..." the
> following things happen, similarly with "X(..)":
> 
>    evaluate("class declaration"):
>        - create an empty class C with C::  dict    set to an
>          object of mapping type.
>        - if there exists a metaclass declaration, set the
>          attribute C::  class  , and C::  dict  ['  metaclass  ]
>          to the metaclass otherwise set C::  class   to type.
>        ...
>        - perform a C3 linearization on all the base classes
>          and assign the result as a tuple to C::  dict  [  mro  ].
>        ...
> 
>    evaluate("instantiate(class object)"):
>        - create an empty object obj with obj::  dict   set to an
>          object of mapping type, and obj::  class   to class object.
>        ...

This is also the way I understand how class instantiation works. Maybe
we are both right (or both wrong ;)

> are you saying simply:
> 
>    definition("super class of C with respect to S") ::= 
>       S.  mro  [index(C)+1]
>

Yes.

> would you entertain dividing it up:
> 
>    linearized(C) ::=
>       C.  mro  
>    super context(A1, A2) ::=
>       remaining = A2.  mro  [index(A1)+1 : ]
>       return remaining
> 
>    linear bases(C) ::=   # more descriptive than ancestor?
>       C.  mro  [1:]   
>    superclass(A1, A2) ::=       # don't have a use case for this one
>       super context(A1, A2)[0]
> 
> then super could be defined easily (and I can't see any obvious problems
> with it, although that might not mean much right now... :-):
> 
>   # Convenient shorthand, mainly to make them notationally
>   # separate from classes and objects (to prevent recursive
>   # deifnitions, can either be implemented as classes/objects
>   # or C structs, etc.
>   RECORD{class tag}(v1,.., vn)
> 
>   super(C, obj) ::=
>       context = super context(C, obj)
>       return RECORD{super object}(context, obj) 
>                 with   class   := obj::  class  
> 
>   super(C) ::=
>       r = RECORD{unbound super}(C)
>       r.  get  (obj,  ):
>           return super(C, obj)
>       return r
>       
>    obj.attr ::=
>       if obj matches RECORD[super object](context, obj)
>           return  get  attribute(obj, context, attr)
>       else:
>           return  get attribute(obj, linearized(obj::  class  ), x)
>           
>    # len(), hash(), int(), ...
>    special(obj) ::=
>       # look it up using regular mechanism
>       hookFn =  get attribute(obj, linearized(obj::  class  ),
> '  special  ')
>       # tell imlementation how to call the hook
>       call special function(hookFn)
> 
>    compute attribute(x, cls, attr) ::=
>       returns cls::  dict  ['  getattr  '](x, attr) if it has a value
>       or raises AttributeError
> 
>    atomic get attribute(x, cls, attr) ::=
>       # how I think it should work (btw., type(x) is a red-herring).
>       
>       Return the first expression that has a value (or raise from
> compute attribute):
>          if attr is normal:
>             x::  dict  [attr]
>             cls::  dict  [attr].  get  (x, cls)
>             cls::  dict  [attr]
>             compute attribute(x, attr)
>          if attr is special:
>             cls::  dict  [attr]
>             cls.  metaclass  ::  dict  [attr].  get  (cls,
> cls.  metaclass  ) ##??
>             cls.  metaclass  ::  dict  [attr]
>             compute attribute(cls, attr)
> 
>    get attribute(x, linearization, attr):
>       for cls in linearization:
>           try:
>               return  atomic get attribute(x, cls, attr)
>           except AttributeError:
>               pass
>        raise AttributeError, attr
> 
> I'm not sure I got the descriptors right, so feel free to criticize...
> 
> -- bjorn

Can you post actual code instead of BNF-like pseudocode, please ? 
I would like to see a real implementation, if possible.


          Michele




More information about the Python-list mailing list