Re: Classes in a class: how to access variables from one in another
- From: Chris Rebert <clp2@xxxxxxxxxxxx>
- Date: Mon, 18 Oct 2010 09:34:07 -0700
On Mon, Oct 18, 2010 at 8:58 AM, Andreas Waldenburger
<usenot@xxxxxxxxxxxxxxxx> wrote:
On Mon, 18 Oct 2010 17:17:52 +0200 Christian Heimes <lists@xxxxxxxxxx>
wrote:
[snip]
Don't nest classes. Just don't. This might be a valid and good
approach in some programming languages but it's not Pythonic.
Explain!
"Private" classes that are closely related to another class can be
simply be defined at the module level with an appropriate name
indicating the privacy (e.g. _Private vs. Public) rather than inside
their associated class; this saves on indentation and is thus more
pleasant to read.
Also, Python's scoping rules, particularly for class-level scopes,
don't work the way programmers from languages where nested classes are
common would expect:
class Foo(object):
SHARED_CONSTANT = 42
class FooBar(object):
def baz(self):
return SHARED_CONSTANT
Foo.FooBar().baz()
==>
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in baz
NameError: global name 'SHARED_CONSTANT' is not defined
Since you must use Foo.SHARED_CONSTANT and similar anyway when you're
in FooBar, nesting FooBar within Foo doesn't really confer any
advantages in the conciseness department.
Cheers,
Chris
--
http://blog.rebertia.com
.
- Follow-Ups:
- Re: Classes in a class: how to access variables from one in another
- From: Steven D'Aprano
- Re: Classes in a class: how to access variables from one in another
- From: Steven D'Aprano
- Re: Classes in a class: how to access variables from one in another
- From: Steven D'Aprano
- Re: Classes in a class: how to access variables from one in another
- From: Steven D'Aprano
- Re: Classes in a class: how to access variables from one in another
- From: Steven D'Aprano
- Re: Classes in a class: how to access variables from one in another
- From: Steven D'Aprano
- Re: Classes in a class: how to access variables from one in another
- From: Steven D'Aprano
- Re: Classes in a class: how to access variables from one in another
- From: Steven D'Aprano
- Re: Classes in a class: how to access variables from one in another
- From: Steven D'Aprano
- Re: Classes in a class: how to access variables from one in another
- From: Steven D'Aprano
- Re: Classes in a class: how to access variables from one in another
- References:
- Classes in a class: how to access variables from one in another
- From: fab
- Re: Classes in a class: how to access variables from one in another
- From: Neil Cerutti
- Re: Classes in a class: how to access variables from one in another
- From: fab
- Re: Classes in a class: how to access variables from one in another
- From: Gary Herron
- Re: Classes in a class: how to access variables from one in another
- From: fab
- Re: Classes in a class: how to access variables from one in another
- From: Christian Heimes
- Re: Classes in a class: how to access variables from one in another
- From: Andreas Waldenburger
- Classes in a class: how to access variables from one in another
- Prev by Date: Re: Classes in a class: how to access variables from one in another
- Next by Date: Re: How to get key values when iterating a mailbox?
- Previous by thread: Re: Classes in a class: how to access variables from one in another
- Next by thread: Re: Classes in a class: how to access variables from one in another
- Index(es):