Best place for a function?

S

Sergio Correia

I'm writing a class, where one of the methods is kinda complex. The
method uses a function which I know for certain will not be used
anywhere else. This function does not require anything from self, only
the args passed by the method.

Where should I put the function?

a) Inside the module but outside the class (to avoid cluttering it;
besides the function does not require to access any property or method
of the class).

# mymodule.py

def _myfunction():
...

class myclass(object):
def mymethod(self):
...
spam = _myfunction()
...


b) Inside the class but outside the method

# mymodule.py

class myclass(object):
def _myfunction(self):
...

def mymethod(self):
...
spam = self._myfunction()
...

c) Inside the method:

# mymodule.py

class myclass(object):
...
def mymethod(self):
def _myfunction(self):
...
...
spam = self._myfunction()
...


I'm new to python (and couldn't find anything about this in PEP 8).
What would you suggest me?

Thanks
Sergio
 
D

Diez B. Roggisch

Sergio said:
I'm writing a class, where one of the methods is kinda complex. The
method uses a function which I know for certain will not be used
anywhere else. This function does not require anything from self, only
the args passed by the method.

Where should I put the function?

a) Inside the module but outside the class (to avoid cluttering it;
besides the function does not require to access any property or method
of the class).

# mymodule.py

def _myfunction():
...

class myclass(object):
def mymethod(self):
...
spam = _myfunction()
...


b) Inside the class but outside the method

# mymodule.py

class myclass(object):
def _myfunction(self):
...

def mymethod(self):
...
spam = self._myfunction()
...

c) Inside the method:

# mymodule.py

class myclass(object):
...
def mymethod(self):
def _myfunction(self):
...
...
spam = self._myfunction()
...


I'm new to python (and couldn't find anything about this in PEP 8).
What would you suggest me?

If it really has no other use as in this class, put it as an
instancemethod in there. Alternatively, you _could_ nest it like this:

class Foo(object):

def bar(self):
def my_long_important_method(argument):
pass
pass


Diez
 
B

bearophileHUGS

Diez B. Roggisch:
If it really has no other use as in this class, put it as an
instancemethod in there. Alternatively, you _could_ nest it like this:

Using an instancemethod may be the most formally correct solution for
that problem, but often nested function are the simpler solution. A
downside of nested functions is that you can't give them a doctest (in
a simple way).

Bye,
bearophile
 
B

Bruno Desthuilliers

Sergio Correia a écrit :
I'm writing a class, where one of the methods is kinda complex. The
method uses a function which I know for certain will not be used
anywhere else. This function does not require anything from self, only
the args passed by the method.

Where should I put the function?

Somewhere in your module ?-)

(snip)

If your problem is to reduce the apparent complexity of the method,
defining the function inside the method won't help that much (unless the
function by itself is short enough - and event then...). Apart from
having a direct access to the method's namespace, you don't gain much by
doing so. And if it's a an implementation function that doesn't need to
access the instance, it has no reason to be a method. Moreover, since
it's not part of neither the class or the module interface, you can
freely change your mind if and when you find a need to do so.
 
S

Sergio Correia

I also found out I can't use `unittest` with nested functions :-(

Thank you all for the responses,

Best,
Sergio
 

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,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top