Classes in a class: how to access variables from one in another

S

Steven D'Aprano

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:
[snip example]


I'm sorry, I don't see that "language Foo programmers will be surprised
that Python is not language Foo" is an argument against Python
programmers using nested classes. Surely it's an argument against writing
Foo in Python?
 
S

Steven D'Aprano

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:
[snip example]


I'm sorry, I don't see that "language Foo programmers will be surprised
that Python is not language Foo" is an argument against Python
programmers using nested classes. Surely it's an argument against writing
Foo in Python?
 
S

Steven D'Aprano

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:
[snip example]


I'm sorry, I don't see that "language Foo programmers will be surprised
that Python is not language Foo" is an argument against Python
programmers using nested classes. Surely it's an argument against writing
Foo in Python?
 
S

Steven D'Aprano

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:
[snip example]


I'm sorry, I don't see that "language Foo programmers will be surprised
that Python is not language Foo" is an argument against Python
programmers using nested classes. Surely it's an argument against writing
Foo in Python?
 
A

Aahz

I'm sorry, I don't see that "language Foo programmers will be surprised
that Python is not language Foo" is an argument against Python
programmers using nested classes. Surely it's an argument against
writing Foo in Python?

Maybe, but there's no reason for posting that ten times! ;-)
 
A

alex23

Steven D'Aprano said:
I'll accept that nested classes are unusual, but unPythonic? Never!

Not even if "flat is better than nested" ;)

And are you the same Steven D'Aprano who once wrote:

"Never nest classes, unless you need to, or to win a bet."

Given that there's little if anything gained from nesting classes
(other than possible scoping confusion) is there ever a need?
 
J

James Mills

Given that there's little if anything gained from nesting classes
(other than possible scoping confusion) is there ever a need?

15+ years of programming and I've never used nested classes in any language :)

cheers
James
 
S

Steven D'Aprano

15+ years of programming and I've never used nested classes in any
language :)

I first started programming in the mid 1980s, and I've never once needed
to write an SQL query. Does that imply that nobody does?
 
C

Chris Rebert

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:
[snip example]

I'm sorry, I don't see that "language Foo programmers will be surprised
that Python is not language Foo" is an argument against Python
programmers using nested classes. Surely it's an argument against writing
Foo in Python?

Touché; however, Python's semantics here differ so drastically from
most other languages and the construction in question is so rarely
used or useful that anyone who does use it is likely either in the
running for Python language-lawyers-of-the-year or has a vendetta
against polyglot programmers. :)

Cheers,
Chris
 
S

Steven D'Aprano

Not even if "flat is better than nested" ;)

But "Namespaces are one honking great idea -- let's do more of
those!" (classes are a good thing) and "Readability counts." (namespace
pollution is a bad thing).

And are you the same Steven D'Aprano who once wrote:

"Never nest classes, unless you need to, or to win a bet."

Yep, that was me. And I stand by it. Most of the time non-nested
solutions is the best solution. But when it's not, there's nothing wrong
with nesting classes. After all, a nested class is no more scary than a
nested function.

Given that there's little if anything gained from nesting classes (other
than possible scoping confusion) is there ever a need?

Avoiding namespace pollution and information hiding are two good reasons
for nesting classes. Take this recipe for example:


http://code.activestate.com/recipes/252524


There's no need to expose the Node class, and no reason not to nest it.
Nor is there particularly any reason to nest it, other than those given
above. But it is a matter of choice -- the author choose not to nest the
class, but he could easily have chosen to. If the author had valued
information hiding over one extra indent level, then I would call that a
need.
 
A

alex23

Steven D'Aprano said:
Avoiding namespace pollution and information hiding are two good reasons
for nesting classes.

Python already has a great mechanism for regulating namespaces:
modules & importing. Information hiding seems to go against the 'all
consenting adults' ethos as well.
There's no need to expose the Node class, and no reason not to nest it.

Well, other than maybe code reuse, that seems a good reason to me.
Being able to modify the behaviour of someone else's class because
they _haven't_ nested secondary class functionality within it would be
a big plus too, even more so if the outer class instead takes a class
or factory as part of its constructor.
 
A

Andreas Waldenburger

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:
[snip example]


I'm sorry, I don't see that "language Foo programmers will be
surprised that Python is not language Foo" is an argument against
Python programmers using nested classes. Surely it's an argument
against writing Foo in Python?

Alright, alright, you made your point.

You should go into politics. ;)

/W
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top