Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Best place for a function?

Reply
Thread Tools

Best place for a function?

 
 
Sergio Correia
Guest
Posts: n/a
 
      03-07-2007
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 .
What would you suggest me?

Thanks
Sergio
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      03-07-2007
Sergio Correia schrieb:
> 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 .
> 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
 
Reply With Quote
 
 
 
 
bearophileHUGS@lycos.com
Guest
Posts: n/a
 
      03-07-2007
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

 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      03-07-2007
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.
 
Reply With Quote
 
Sergio Correia
Guest
Posts: n/a
 
      03-07-2007
I also found out I can't use `unittest` with nested functions

Thank you all for the responses,

Best,
Sergio

On 7 Mar 2007 14:57:54 -0800,
<> wrote:
> 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
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Best Place Of Training for MCSE Dave Marden Microsoft Certification 1 04-10-2006 03:49 PM
Best place to jack off on the internet Consultant MCSD 0 08-31-2004 06:52 AM
Re: Best place for exchanging $ to Euro? mrtravelkay Cisco 0 04-23-2004 11:50 PM
Where is best place for best price buying Mobo & CPU combo? Arawak Computer Support 6 02-05-2004 04:46 PM
Best place to hold dataset between postbacks? Gandalf ASP .Net 1 07-17-2003 11:28 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57