AFAIK the point of a class method is to call it directly from the class
(think of it as a __new__ equivalent plus the ability to define custom
signatures), you can also call it from the instance in which case your
example is correct, but if you do so it will be trickier to take into
account this duality while @staticmethod does exactly what Rudi was asking
for (no instance, no class, no side effects... just a static function
defined within the namespace of the class).

>>> class Foo(object):
...     @classmethod
...     def fancy_constructor(cls, is_valid=True):
...         if is_valid:
...             return cls()
...
>>> class1 = Foo()
>>> class2 = Foo.fancy_constructor(True)
>>> class3 = Foo.fancy_constructor(False)
>>>
>>> print class1, class2, class3
<__main__.Foo object at 0x7fca725fb7d0> <__main__.Foo object at
0x7fca725fb710> None


On Mon, Jun 6, 2016 at 11:46 PM, Marcus Ottosson <[email protected]>
wrote:

> > Nope, a class method take the class as first argument and is supposed to
> return an instance of said class
>
> Just so we don't confuse Rudi, a @classmethod is like a regular method,
> except that it doesn't have access to its instance. There are no
> expectation or restrictions on what it returns.
>
> class MyClass(object):
>   def regular_method(self):
>     return self
>
>   @classmethod
>   def class_method(cls):
>     return cls
>
> myclass1 = MyClass()
> myclass2 = MyClass()
>
> assert myclass1.regular_method() != myclass2.regular_method()
> assert myclass2.class_method() == myclass2.class_method()
> ​
>
> --
> You received this message because you are subscribed to the Google Groups
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOAkM4%3DrH%2By14JmsiZXDFTBZNxT30wmc5VvvQo-DhZ6EPg%40mail.gmail.com
> <https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOAkM4%3DrH%2By14JmsiZXDFTBZNxT30wmc5VvvQo-DhZ6EPg%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPamJi8KYgivv-1w%3DUV0k2uU4q%3Dg9w%2BOAcD6q%3DJ0YUMOvCdZSQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to