Classes: nested functions vs. private methodes

Discussion in 'Python' started by Richard Lamboj, May 6, 2010.

  1. Hello,

    what should i take:
    - nested functions:
    class MyClass(object)
    def blah(self):
    def blub(var1, var2):
    do something...
    blub(1, 5)

    or

    class MyClass(object)
    def blah(self):
    def _blub(var1, var2):
    do something...
    _blub(1, 5)

    - "private" functions:
    class MyClass(object)
    def blah(self):
    self._blub()
    def _blub(self):
    do something...

    What is more pythonic?

    It is just for splitting up long code in classes.

    Kind Regards,

    Richi
    Richard Lamboj, May 6, 2010
    #1
    1. Advertising

  2. On Thu, 06 May 2010 11:24:49 +0200, Richard Lamboj wrote:

    > Hello,
    >
    > what should i take:
    > - nested functions:
    > class MyClass(object)
    > def blah(self):
    > def blub(var1, var2):
    > do something...
    > blub(1, 5)



    The disadvantage of nested functions is that it is harder to test them in
    isolation.



    > or
    >
    > class MyClass(object)
    > def blah(self):
    > def _blub(var1, var2):
    > do something...
    > _blub(1, 5)



    There is no real point in marking a nested function as "private" with a
    leading underscore, as no other object can get access to it.



    > - "private" functions:
    > class MyClass(object)
    > def blah(self):
    > self._blub()
    > def _blub(self):
    > do something...




    This has the advantage of allowing you to test blah() and _blub() in
    isolation. It has the disadvantage of polluting the namespace of MyClass
    with an extra visible method, _blub.


    > What is more pythonic?


    Both are Pythonic. Private methods are more object oriented, nested
    functions are more procedural, but it is mostly a matter of taste which
    you use. You can use either, or a combination of both, or even do this:


    class _MyClass(object):
    """Private class blah blah blah..."""
    def blah(self, arg):
    do something...

    class MyClass(_MyClass):
    """Public interface to _MyClass"""
    def blah(self, arg):
    arg = preprocessing(arg)
    x = super(MyClass, self).blah(arg)
    x = postprocessing(x)
    return x



    --
    Steven
    Steven D'Aprano, May 6, 2010
    #2
    1. Advertising

  3. Am Thursday 06 May 2010 12:02:47 schrieb Steven D'Aprano:
    > On Thu, 06 May 2010 11:24:49 +0200, Richard Lamboj wrote:
    > > Hello,
    > >
    > > what should i take:
    > > - nested functions:
    > > class MyClass(object)
    > > def blah(self):
    > > def blub(var1, var2):
    > > do something...
    > > blub(1, 5)

    >
    > The disadvantage of nested functions is that it is harder to test them in
    > isolation.
    >
    > > or
    > >
    > > class MyClass(object)
    > > def blah(self):
    > > def _blub(var1, var2):
    > > do something...
    > > _blub(1, 5)

    >
    > There is no real point in marking a nested function as "private" with a
    > leading underscore, as no other object can get access to it.
    >
    > > - "private" functions:
    > > class MyClass(object)
    > > def blah(self):
    > > self._blub()
    > > def _blub(self):
    > > do something...

    >
    > This has the advantage of allowing you to test blah() and _blub() in
    > isolation. It has the disadvantage of polluting the namespace of MyClass
    > with an extra visible method, _blub.
    >
    > > What is more pythonic?

    >
    > Both are Pythonic. Private methods are more object oriented, nested
    > functions are more procedural, but it is mostly a matter of taste which
    > you use. You can use either, or a combination of both, or even do this:
    >
    >
    > class _MyClass(object):
    > """Private class blah blah blah..."""
    > def blah(self, arg):
    > do something...
    >
    > class MyClass(_MyClass):
    > """Public interface to _MyClass"""
    > def blah(self, arg):
    > arg = preprocessing(arg)
    > x = super(MyClass, self).blah(arg)
    > x = postprocessing(x)
    > return x
    >
    >
    >
    > --
    > Steven


    Thank you for the nice sample, but what is with multiple inheritance in your
    sample? I mean the super call. Why not _MyClass.blah(self, arg). What is when
    i have more then one Class from which i inherite and in both are a methode
    called "blah", but i just want to call one of them and not both, becouse they
    do different things?

    Kind Regards,

    Richi
    Richard Lamboj, May 6, 2010
    #3
  4. On Thu, 06 May 2010 12:40:16 +0200, Richard Lamboj wrote:

    > Thank you for the nice sample, but what is with multiple inheritance in
    > your sample? I mean the super call. Why not _MyClass.blah(self, arg).


    super() should work correctly when you have more complicated multiple
    inheritance, while calling the parent class directly may not.

    Of course you can call _MyClass.blah if you prefer, for single
    inheritance it should be good enough.


    > What is when i have more then one Class from which i inherite and in
    > both are a methode called "blah", but i just want to call one of them
    > and not both, becouse they do different things?


    super() will not call both, it will call the first method that matches.

    Unfortunately, multiple inheritance is complicated. Here is a good
    article on super():

    http://www.artima.com/weblogs/viewpost.jsp?thread=236275


    And for fairness, here is an article which argues that super() is harmful:

    http://fuhm.net/super-harmful/


    but really, it seems to me that he is arguing that super() is hard to get
    right, not harmful, and that is not because super() is hard, but because
    multiple inheritance is hard.



    --
    Steven
    Steven D'Aprano, May 7, 2010
    #4
    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. Chad E. Dollins
    Replies:
    3
    Views:
    645
    Kai-Uwe Bux
    Nov 8, 2005
  2. Replies:
    7
    Views:
    336
  3. earthwormgaz
    Replies:
    6
    Views:
    348
    Emmanuel Deloget
    Nov 29, 2006
  4. John Goche
    Replies:
    10
    Views:
    720
    Marcus Kwok
    Dec 8, 2006
  5. Chris Gordon-Smith
    Replies:
    20
    Views:
    895
    Daniel Pitts
    May 29, 2008
Loading...

Share This Page