From: tismer at appliedbiometrics.com (Christian Tismer)
Date: Mon, 5 Apr 1999 15:35:26 GMT
Subject: Subattributes of classes
References: <19990405104408.A807008@vislab.epa.gov>
Message-ID: <3708D83E.BBE87D60@appliedbiometrics.com>
Content-Length: 1448
X-UID: 972                                                  


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




