How can I define class methods outside of the class?

Discussion in 'Python' started by Jeremy, Dec 2, 2010.

  1. Jeremy

    Jeremy Guest

    I have some methods that I need (would like) to define outside of the
    class. I know this can be done by defining the function and then
    setting it equal to some member of an instance of the class. But,
    because of the complexity of what I'm doing (I have to set many
    functions as class methods) I would rather not do this. Can someone
    show me how to do this? Is it even possible? Can decorators be used
    here?

    Thanks,
    Jeremy
    Jeremy, Dec 2, 2010
    #1
    1. Advertising

  2. Jeremy

    James Mills Guest

    On Thu, Dec 2, 2010 at 3:36 PM, Jeremy <> wrote:
    > I have some methods that I need (would like) to define outside of the
    > class.  I know this can be done by defining the function and then
    > setting it equal to some member of an instance of the class.  But,
    > because of the complexity of what I'm doing (I have to set many
    > functions as class methods) I would rather not do this.  Can someone
    > show me how to do this?  Is it even possible?  Can decorators be used
    > here?


    Do you mean something like this ?

    @classmethod
    def foo(cls):
    print "I am the foo classmethod on %r" % cls

    class Foo(object):
    pass

    Foo.foo = foo

    cheers
    James

    --
    -- James Mills
    --
    -- "Problems are solved by method"
    James Mills, Dec 2, 2010
    #2
    1. Advertising

  3. Jeremy

    Ian Kelly Guest

    On 12/1/2010 10:47 PM, James Mills wrote:
    > On Thu, Dec 2, 2010 at 3:36 PM, Jeremy<> wrote:
    >> I have some methods that I need (would like) to define outside of the
    >> class. I know this can be done by defining the function and then
    >> setting it equal to some member of an instance of the class. But,
    >> because of the complexity of what I'm doing (I have to set many
    >> functions as class methods) I would rather not do this. Can someone
    >> show me how to do this? Is it even possible? Can decorators be used
    >> here?

    >
    > Do you mean something like this ?
    >
    > @classmethod
    > def foo(cls):
    > print "I am the foo classmethod on %r" % cls
    >
    > class Foo(object):
    > pass
    >
    > Foo.foo = foo


    Slightly better:

    def foo(cls):
    # Do something

    class Foo(object):
    pass

    Foo.foo = classmethod(foo)

    This way the unbound foo is callable as well.

    Cheers,
    Ian
    Ian Kelly, Dec 2, 2010
    #3
  4. Jeremy

    Jeremy Guest

    On Dec 1, 10:47 pm, James Mills <> wrote:
    > On Thu, Dec 2, 2010 at 3:36 PM, Jeremy <> wrote:
    > > I have some methods that I need (would like) to define outside of the
    > > class.  I know this can be done by defining the function and then
    > > setting it equal to some member of an instance of the class.  But,
    > > because of the complexity of what I'm doing (I have to set many
    > > functions as class methods) I would rather not do this.  Can someone
    > > show me how to do this?  Is it even possible?  Can decorators be used
    > > here?

    >
    > Do you mean something like this ?
    >
    > @classmethod
    > def foo(cls):
    >     print "I am the foo classmethod on %r" % cls
    >
    > class Foo(object):
    >     pass
    >
    > Foo.foo = foo
    >
    > cheers
    > James


    Thanks, James. That is almost exactly what I want. However, I want
    to avoid doing

    Foo.foo = foo

    Is this going to be possible? I'm trying to understand how decorators
    are used. Are they really necessary in this example?

    Thanks,
    Jeremy
    Jeremy, Dec 2, 2010
    #4
  5. Jeremy

    Guest

    On 2 déc, 06:36, Jeremy <> wrote:
    > I have some methods that I need (would like) to define outside of the
    > class.  I know this can be done by defining the function and then
    > setting it equal to some member


    <OT>
    "assignement" or "binding" might be the terms you were looking for
    here ;)

    Also in Python we talk about "attributes", not "members"
    </OT>

    > of an instance of the class.


    What you describe here will not "define class methods", nor even
    instance methods FWIW - it will only make the function an attribute of
    the instance, but won't turn the function into a method (IOW: the
    function won't get the instance as first argument).

    Also and while we're at it, a Python "classmethod" is something
    special - it's a method that can be called on either an instance or
    the class itself, and takes the class - not the instance - as first
    argument.

    > But,
    > because of the complexity of what I'm doing (I have to set many
    > functions as class methods) I would rather not do this.  Can someone
    > show me how to do this?  Is it even possible?


    To "no do this" ? Yes, certainly <g>

    More seriously: if your problem is to dynamically add a bunch of
    methods to an existing *class*, it's quite easy - just import the
    class and assign your functions to it, ie:

    from otherlib import OtherClass

    def foo(self):
    print "%s.foo" % self


    OtherClass.foo = foo

    And voila, The "foo" method is now available to all (even already
    existing) instances of OtherClass.

    If this doesn't answer your question, please provide more context.

    >  Can decorators be used
    > here?


    What for ?
    , Dec 2, 2010
    #5
  6. Jeremy

    Guest

    On 2 déc, 15:45, Jeremy <> wrote:
    > On Dec 1, 10:47 pm, James Mills <> wrote:
    >
    >
    >
    > > On Thu, Dec 2, 2010 at 3:36 PM, Jeremy <> wrote:
    > > > I have some methods that I need (would like) to define outside of the
    > > > class.  I know this can be done by defining the function and then
    > > > setting it equal to some member of an instance of the class.  But,
    > > > because of the complexity of what I'm doing (I have to set many
    > > > functions as class methods) I would rather not do this.  Can someone
    > > > show me how to do this?  Is it even possible?  Can decorators be used
    > > > here?

    >
    > > Do you mean something like this ?

    >
    > > @classmethod
    > > def foo(cls):
    > >     print "I am the foo classmethod on %r" % cls

    >
    > > class Foo(object):
    > >     pass

    >
    > > Foo.foo = foo

    >
    > > cheers
    > > James

    >
    > Thanks, James.  That is almost exactly what I want.  However, I want to avoid doing
    >
    > Foo.foo = foo
    >
    > Is this going to be possible?  


    def patch(cls):
    def _patch(func):
    setattr(cls, func.__name__, func)
    return func
    return _patch

    class Foo(object): pass


    @patch(Foo)
    def bar(self):
    print self

    f = Foo()
    f.bar()


    > I'm trying to understand how decorators
    > are used.  Are they really necessary in this example?


    In the above example, the classmethod type was used as a decorator to
    turn the function into, well, a classmethod (read my other answer in
    this thread if you don't know what a classmethod is).
    , Dec 2, 2010
    #6
  7. Jeremy

    Jeremy Guest

    On Dec 2, 10:26 am, ""
    <> wrote:
    > On 2 déc, 15:45, Jeremy <> wrote:
    >
    >
    >
    >
    >
    > > On Dec 1, 10:47 pm, James Mills <> wrote:

    >
    > > > On Thu, Dec 2, 2010 at 3:36 PM, Jeremy <> wrote:
    > > > > I have some methods that I need (would like) to define outside of the
    > > > > class.  I know this can be done by defining the function and then
    > > > > setting it equal to some member of an instance of the class.  But,
    > > > > because of the complexity of what I'm doing (I have to set many
    > > > > functions as class methods) I would rather not do this.  Can someone
    > > > > show me how to do this?  Is it even possible?  Can decorators be used
    > > > > here?

    >
    > > > Do you mean something like this ?

    >
    > > > @classmethod
    > > > def foo(cls):
    > > >     print "I am the foo classmethod on %r" % cls

    >
    > > > class Foo(object):
    > > >     pass

    >
    > > > Foo.foo = foo

    >
    > > > cheers
    > > > James

    >
    > > Thanks, James.  That is almost exactly what I want.  However, I want to avoid doing

    >
    > > Foo.foo = foo

    >
    > > Is this going to be possible?  

    >
    > def patch(cls):
    >    def _patch(func):
    >        setattr(cls, func.__name__, func)
    >        return func
    >    return _patch
    >
    > class Foo(object): pass
    >
    > @patch(Foo)
    > def bar(self):
    >     print self
    >
    > f = Foo()
    > f.bar()


    Yes! This is what I was looking for. Thanks!

    Jeremy
    Jeremy, Dec 2, 2010
    #7
    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. Lord Landon
    Replies:
    1
    Views:
    291
    Peter Otten
    May 4, 2006
  2. Replies:
    4
    Views:
    589
  3. Replies:
    5
    Views:
    354
  4. Kenneth McDonald
    Replies:
    5
    Views:
    312
    Kenneth McDonald
    Sep 26, 2008
  5. Krzysztof Poc

    outside type, outside function

    Krzysztof Poc, Feb 3, 2012, in forum: C++
    Replies:
    1
    Views:
    282
    Victor Bazarov
    Feb 7, 2012
Loading...

Share This Page