nested classes

Discussion in 'Python' started by Esmail, Mar 20, 2009.

  1. Esmail

    Esmail Guest

    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
     
    Esmail, Mar 20, 2009
    #1
    1. Advertising

  2. Esmail

    Steve Holden Guest

    Benjamin Kaplan wrote:
    >
    >
    > On Fri, Mar 20, 2009 at 10:06 AM, Esmail <
    > <mailto:>> wrote:
    >
    > 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
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/
    Want to know? Come to PyCon - soon! http://us.pycon.org/
     
    Steve Holden, Mar 20, 2009
    #2
    1. Advertising

  3. Esmail

    Chris Rebert Guest

    2009/3/20 Benjamin Kaplan <>:
    >
    >
    > On Fri, Mar 20, 2009 at 10:06 AM, Esmail <> wrote:
    >>
    >> 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.


    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

    --
    I have a blog:
    http://blog.rebertia.com
     
    Chris Rebert, Mar 20, 2009
    #3
  4. Esmail

    Esmail Guest

    On Mar 20, 2:41 pm, Chris Rebert <> wrote:
    > 2009/3/20 Benjamin Kaplan <>:
    >
    >
    >
    >
    >
    > > On Fri, Mar 20, 2009 at 10:06 AM, Esmail <> wrote:

    >
    > >> 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.

    >
    > 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"?
     
    Esmail, Mar 20, 2009
    #4
  5. Esmail

    Esmail Guest

    On Mar 20, 2:35 pm, Steve Holden <> wrote:
    > Benjamin Kaplan wrote:
    >
    > > On Fri, Mar 20, 2009 at 10:06 AM, Esmail <
    > > <mailto:>> wrote:

    >
    > >     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.


    Thanks Steve,

    Esmail
     
    Esmail, Mar 20, 2009
    #5
  6. Esmail

    Chris Rebert Guest

    On Fri, Mar 20, 2009 at 1:24 PM, Esmail <> wrote:
    > On Mar 20, 2:41 pm, Chris Rebert <> wrote:
    >> 2009/3/20 Benjamin Kaplan <>:
    >> > On Fri, Mar 20, 2009 at 10:06 AM, Esmail <> wrote:

    >>
    >> >> 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.

    >>
    >> 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"?


    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

    --
    I have a blog:
    http://blog.rebertia.com
     
    Chris Rebert, Mar 20, 2009
    #6
  7. Esmail

    Esmail Guest


    > 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
     
    Esmail, Mar 20, 2009
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Razvan
    Replies:
    5
    Views:
    11,311
    Dale King
    Jul 27, 2004
  2. kelvSYC
    Replies:
    2
    Views:
    595
    Chris Smith
    Aug 18, 2004
  3. Russ Perry Jr
    Replies:
    2
    Views:
    4,148
    Russ Perry Jr
    Aug 20, 2004
  4. Tony Johansson

    Nested classes within other classes

    Tony Johansson, Dec 14, 2004, in forum: C++
    Replies:
    2
    Views:
    483
    msalters
    Dec 14, 2004
  5. Chad E. Dollins
    Replies:
    3
    Views:
    659
    Kai-Uwe Bux
    Nov 8, 2005
Loading...

Share This Page