nested classes

E

Esmail

Hello all,

I am curious why nested classes don't seem to be used much in Python.
I see them as a great way to encapsulate related information, which is
a
good thing.

In my other post "improve this newbie code/nested functions in
Python?"
(I accidentally referred to nested functions rather nested classes -
it was late)
I asked something similar in the context of a specific example where I
think the
use of nested classes makes sense.

But perhaps not?

Esmail
 
S

Steve Holden

Benjamin said:
On Fri, Mar 20, 2009 at 10:06 AM, Esmail <[email protected]

Hello all,

I am curious why nested classes don't seem to be used much in Python.
I see them as a great way to encapsulate related information, which is
a
good thing.

In my other post "improve this newbie code/nested functions in
Python?"
(I accidentally referred to nested functions rather nested classes -
it was late)
I asked something similar in the context of a specific example where I
think the
use of nested classes makes sense.

But perhaps not?


Nested classes in Python don't add much other than an additional level
of complexity (and an extra hash lookup). Behavior in python is usually
grouped into modules, not into classes. The only reason to nest a class
in Python is if the first class is going to generate the second class on
the fly.
And even then you;d nest it inside a method of the class.

regards
Steve
 
C

Chris Rebert

2009/3/20 Benjamin Kaplan said:
Nested classes in Python don't add much other than an additional level of
complexity (and an extra hash lookup). Behavior in python is usually grouped
into modules, not into classes. The only reason to nest a class in Python is
if the first class is going to generate the second class on the fly.

Verily. See also the principle that "Flat is better than nested" from
the Zen of Python (http://www.python.org/dev/peps/pep-0020/).
The OP would be better off naming internal classes with leading
underscores per Python convention rather than nesting them inside
other classes.

Cheers,
Chris
 
E

Esmail

2009/3/20 Benjamin Kaplan <[email protected]>:







Verily. See also the principle that "Flat is better than nested" from
the Zen of Python (http://www.python.org/dev/peps/pep-0020/).

Neat list .. thanks .. just what I'm looking for. I am trying to learn
the idioms of the language, this will help.
The OP would be better off naming internal classes with leading
underscores per Python convention rather than nesting them inside
other classes.

So you would make them "stand-alone/external" classes but "tag" them
with the underscore to document that they are used by some other
classes
as "internal service providers"?
 
C

Chris Rebert

Neat list .. thanks .. just what I'm looking for. I am trying to learn
the idioms of the language, this will help.


So you would make them "stand-alone/external" classes but "tag" them
with the underscore to document that they are used by some other
classes
as "internal service providers"?

Yes. It's the same convention used to indicate that a method is
"private" in Python, since the language itself has no privacy
mechanisms.

Cheers,
Chris
 
E

Esmail

Yes. It's the same convention used to indicate that a method is
"private" in Python, since the language itself has no privacy
mechanisms.

Great - got it!

Thanks again,
Esmail
 

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
473,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top