Best place for a function?

Discussion in 'Python' started by Sergio Correia, Mar 7, 2007.

  1. 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
    Sergio Correia, Mar 7, 2007
    #1
    1. Advertising

  2. 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 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
    Diez B. Roggisch, Mar 7, 2007
    #2
    1. Advertising

  3. Sergio Correia

    Guest

    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
    , Mar 7, 2007
    #3
  4. 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.
    Bruno Desthuilliers, Mar 7, 2007
    #4
  5. 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
    >
    Sergio Correia, Mar 7, 2007
    #5
    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. Gandalf
    Replies:
    1
    Views:
    443
    Steve C. Orr, MCSD
    Jul 18, 2003
  2. Leo Muller
    Replies:
    4
    Views:
    3,764
    Josh Harris
    Feb 17, 2004
  3. Schoo
    Replies:
    5
    Views:
    3,848
    Steven Cheng[MSFT]
    Mar 3, 2004
  4. Arvind P Rangan

    Where is best place to put DB connection string?

    Arvind P Rangan, Mar 1, 2004, in forum: ASP .Net
    Replies:
    8
    Views:
    607
    Martin Dechev
    Mar 1, 2004
  5. Inyeol Lee

    Re: Best place for a function?

    Inyeol Lee, Mar 9, 2007, in forum: Python
    Replies:
    2
    Views:
    273
    Inyeol Lee
    Mar 13, 2007
Loading...

Share This Page