Mailing List Archive

Subattributes of classes
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?

Randall
Subattributes of classes [ In reply to ]
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@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
Subattributes of classes [ In reply to ]
--pgp-sign-Multipart_Mon_Apr__5_11:42:04_1999-1
Content-Type: text/plain; charset=US-ASCII

Then <aa8vb@vislab.epa.gov> spoke up and said:
>
> 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

because in the demo you pointed to, they are setting sub-attributes of
an integer. The object 123 is immutable. Do it this way:

class foo: pass
f = foo()
f.bar = foo()
f.bar.baz = 123

--
=====================================================================
| JAVA must have been developed in the wilds of West Virginia. |
| After all, why else would it support only single inheritance?? |
=====================================================================
| Finger geek@cmu.edu for my public key. |
=====================================================================

--pgp-sign-Multipart_Mon_Apr__5_11:42:04_1999-1
Content-Type: application/pgp-signature
Content-Transfer-Encoding: 7bit

-----BEGIN PGP MESSAGE-----
Version: 2.6.2
Comment: Processed by Mailcrypt 3.3, an Emacs/PGP interface

iQBVAwUBNwjZzIdzVnzma+gdAQGGEQIAtS2NTa/kl1kEHBcEhX9zGJ2h3J0Cmgmc
Ti1vhi5Ncugf0qDcoGDDtEw+YkQVVCWwBgcvhZUhl/698EwhhmVsoQ==
=28ki
-----END PGP MESSAGE-----

--pgp-sign-Multipart_Mon_Apr__5_11:42:04_1999-1--
Subattributes of classes [ In reply to ]
On Mon, 5 Apr 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.
[skipped]
> Why?

Try this:

class B:
pass

class A:
def __init__( self ):
self.attr = B()
self.attr.subattr = 456

a = A()

> Randall
>
>
>

Oleg.
----
Oleg Broytmann National Research Surgery Centre http://sun.med.ru/~phd/
Programmers don't die, they just GOSUB without RETURN.
Subattributes of classes [ In reply to ]
Randall Hopper wrote:

> Why?

Because "attr" in class A is just an int, and has no attributes,
whereas "draw" in the Tkinter demo is a Canvas object, which has a
scrollX and scrollY attribute.

Consider this example:

class B:
def __init__(self):
self.subattr = None

class A:
def __init__(self):
self.attr = B()
self.attr.subattr = 456

a = A()

print a.attr.subattr
Subattributes of classes [ In reply to ]
Christian Tismer:
|> 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)
|> ^^^^^^^^^^^^
|
|This is since the self.draw is assigned a Canvas instance
|which is itself an object with attributes.

Thanks to all who replied.

I was foiled by "print self.draw" printing something that looked like a
primitive string type (the Tk widget name), and so I didn't think it was a
class and thus couldn't have attributes.

I'll not forget __str__/__repr__ so easily next time. :-)

Thanks,

Randall
Subattributes of classes [ In reply to ]
--pgp-sign-Multipart_Mon_Apr__5_12:36:53_1999-1
Content-Type: text/plain; charset=US-ASCII

Then <cgw@fnal.gov> spoke up and said:
> Randall Hopper wrote:
>
> > Why?
>
> Because "attr" in class A is just an int, and has no attributes,
> whereas "draw" in the Tkinter demo is a Canvas object, which has a
> scrollX and scrollY attribute.

Actually, this is both right and wrong. It doesn't matter whether or
not the object has attributes. What matters is whether or not the
object is mutable. An integer object is not mutable. This is one of
the reasons there is no increment operator for integers. Such an
operation is usually semantically "in place", where it would have to
be a factory function (like +) in Python.

--
=====================================================================
| JAVA must have been developed in the wilds of West Virginia. |
| After all, why else would it support only single inheritance?? |
=====================================================================
| Finger geek@cmu.edu for my public key. |
=====================================================================

--pgp-sign-Multipart_Mon_Apr__5_12:36:53_1999-1
Content-Type: application/pgp-signature
Content-Transfer-Encoding: 7bit

-----BEGIN PGP MESSAGE-----
Version: 2.6.2
Comment: Processed by Mailcrypt 3.3, an Emacs/PGP interface

iQBVAwUBNwjmqIdzVnzma+gdAQGY3wH/UAvHxTLBLcae2nUpWEPGi43gNIiZs/rB
1CzeZJkLJLC6G0yyY+MByzDa6Rs520OiNllpgQe+g0yfkaq6TgwvFw==
=OO/i
-----END PGP MESSAGE-----

--pgp-sign-Multipart_Mon_Apr__5_12:36:53_1999-1--