how to tell a method is classmethod or static method or instance method
Zheng Li
dllizheng at gmail.com
Tue Feb 14 20:46:48 EST 2012
thank you.
I know the second way works.
but in my case, i need "method1" to be a class method, because I use it to create an object.
I have a lot of classes that have "__init__" with 2 arguments -- self, and user id.
usually, "SomeClass(user_id)" is used to create an object of "SomeClass",
but if "SomeClass" has a class method named "method1", "method1" will be used to finish the job.
and i get it. the context is useful.
On 2012/02/14, at 18:52, Cameron Simpson wrote:
> On 14Feb2012 13:13, Zheng Li <dllizheng at gmail.com> wrote:
> | > On 13Feb2012 15:59, Zheng Li <dllizheng at gmail.com> wrote:
> | > | how to tell a method is class method or static method or instance method?
> | >
> | > Maybe a better question is:
> | > under what circumstances do you need to figure this out?
> |
> | I can get "method1" of class "Test" by
> | a = getattr(Test, "method1")
> |
> | and I also want know how to invoke it
> | a() or a(Test())
>
> Normally:
>
> a(T)
>
> where T is an object of type/class Test. So your second approach is
> notionally correct (aside from making a throwaway object that is then
> discarded).
>
> | BTW:
> | I don't see what the problem is if I ask a question just because I am curious about it.
>
> There's nothing wrong with it at all.
>
> But often, questions arise from some other circumstances and this one is
> of such a flavour that if you wanted this code in a real application it
> would _often_ be the wrong solution to seek, because normally you know
> how to call something - it is not normally useful to introspect it to
> decide what to do.
>
> So I was wondering what the outer context might be, because there may
> well have been a better solution to the situation that brought up the
> specific question.
>
> Simple curiosity is sufficient reason, of course.
> --
> Cameron Simpson <cs at zip.com.au> DoD#743
> http://www.cskk.ezoshosting.com/cs/
>
> Too young to rest on the weekend, too old to rest during the week.
> - Mark Randol <ryvw50 at email.sps.mot.com>
More information about the Python-list
mailing list