[Tutor] what is @classmethod and @staticmethod ??
Tony Cappellini
cappy2112 at gmail.com
Mon Mar 24 01:07:55 CET 2008
> I don't use classmethods so I can't discuss that. For staticmethods,
> suppose I have in foo.py
Where is the word "staticmethod" in the example below? Where is it used?
This is what I was hoping to see.
>
> class Foo(object):
> # Lots of useful stuff
>
>
> In client.py I have
>
> from foo import Foo
>
> # Do interesting things with Foo
>
>
> Now perhaps I need a function
> doSomethingRelatedToFoo()
> that belongs in foo.py but doesn't have to be an instance method - it is
> just a related function. I could make this a module function and change
> client.py to read
>
> from foo import Foo, doSomethingRelatedToFoo
>
> doSomethingRelatedToFoo()
>
>
> or I could make doSomethingRelatedToFoo a staticmethod, then I don't
> have to change the import statement, I can access
> doSomethingRelatedToFoo() through the already-imported Foo class:
>
> Foo.doSomethingRelatedToFoo()
>
> It's a pretty small difference but I like keeping the import simple and
> not having to change it when I add doSomethingRelatedToFoo() to foo.py.
>
> Kent
>
More information about the Tutor
mailing list