Classes: nested functions vs. private methodes

R

Richard Lamboj

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
 
S

Steven D'Aprano

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
 
R

Richard Lamboj

Am Thursday 06 May 2010 12:02:47 schrieb Steven D'Aprano:
The disadvantage of nested functions is that it is harder to test them in
isolation.


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.


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.


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

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
 
S

Steven D'Aprano

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.
 

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

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top