Subattributes of classes

Christian Tismer tismer at appliedbiometrics.com
Mon Apr 5 11:35:26 EDT 1999


Randall Hopper wrote:
> 
>    class A:
>      def __init__( self ):
>        self.attr = 123
>        self.attr.subattr = 456        # <--------- Error!
> 
>    a = A()
> 
> This generates an error as I would have expected.
> 
> However, I see the same syntax in:
>    demos/tkinter/matt/canvas-with-scrollbars.py
> 
> and it works:
> 
>    self.draw = Canvas(self, width="5i", height="5i",
>                       background="white",
>                       scrollregion=(0, 0, "20i", "20i"))
>    self.draw.scrollX = Scrollbar(self, orient=HORIZONTAL)
>    self.draw.scrollY = Scrollbar(self, orient=VERTICAL)
> 
>         ^^^^^^^^^^^^
> 
> Why?

This is since the self.draw is assigned a Canvas instance
which is itself an object with attributes.
Here comes your ticket :-)

>>> class attribs: pass
>>> class A:
... 	def __init__(self):
... 	  self.attr = attribs()
... 	  self.attr.subattr = 456
... 	
>>> x=A()
>>> x.attr
<__main__.attribs instance at 15f87e0>
>>> x.attr.subattr
456
>>> 

ciao - chris

-- 
Christian Tismer             :^)   <mailto:tismer at appliedbiometrics.com>
Applied Biometrics GmbH      :     Have a break! Take a ride on Python's
Kaiserin-Augusta-Allee 101   :    *Starship* http://starship.python.net
10553 Berlin                 :     PGP key -> http://wwwkeys.pgp.net
PGP Fingerprint       E182 71C7 1A9D 66E9 9D15  D3CC D4D7 93E2 1FAE F6DF
     we're tired of banana software - shipped green, ripens at home




More information about the Python-list mailing list