On Wed, Apr 15, 2020, 11:01 PM HarshadB <bari.hars...@gmail.com> wrote:

> @alok
> Thank you for your reply.
>
> I already have working class constructors(classmethods) that do what I
> want. That part is done.
> Sorry, I should have asked the question clearly in the first line.
>
> What I am actually doing is to further automate the instance creation of
> the class based on the argument provided, or with no argument.
> For e.g with no argument, it should call cls.from_selection(cls)
> This part is also done but in a hackish sort of way. I have isinstance
> checks in my class __new__ method. *I want to write this code in a
> better, efficient way.*
>
>
> *Problem is when the argument is checked in __new__:*- A class is
> returned from the respective classmethod and passed to init.
> - The original argument is still passed to init after already passing it
> to the classmethod above.
> - So the init gets called twice.
>

Since you are combining classmethod and __new__, you would have to
implement your classmethod to also use super().__new__ so that it doesn't
call the constructor. But that is a bit unintuitive if it is a public
classmethod. So maybe you should define private class methods that can be
called by your __new__.


- I have worked around this for now by having a *isinstance(arg,
> om.mObject)* check in the __init__.
>
> - H
>
> On Wednesday, April 15, 2020 at 3:35:30 PM UTC+9, Alok Gandhi wrote:
>>
>> Python @classmethod were added for this exact reason - Alternative
>> Contructors.
>>
>> class Foo(object):
>>     def __init__(self, bar=None, baz=None):
>>         self._bar = bar
>>         self._baz = baz
>>         print 'bar: {}, baz: {}'.format(self._bar, self._baz)
>>
>>     @classmethod
>>     def from_bar(cls, bar):
>>         return cls(bar=bar)
>>
>>     @classmethod
>>     def from_baz(cls, baz):
>>         return cls(baz=baz)
>>
>> # Default
>> foo = Foo('default bar', 'default baz')
>> # bar: default bar, baz: default baz
>>
>> # From bar
>> foo_bar = Foo.from_bar('from bar')
>> # bar: from bar, baz: None
>>
>> # From baz
>> foo_baz = Foo.from_baz('from baz')
>> # bar: None, baz: from baz
>>
>>
>> - Alok
>>
>> On Wed, Apr 15, 2020, 10:44 HarshadB <bari....@gmail.com> wrote:
>>
>>> Hoping for some help with this, I am trying to understand Class
>>> construction(instantiation before init).
>>>
>>> I have a component class which encapsulates a hierarchy of nodes in
>>> Maya. Instantiated using a mObject as input.
>>> BaseComponent(mObject)
>>>
>>> I want to automate the instance construction of this class based on
>>> selection, mObjectHandle, string and have created classmethods (as
>>> alternative constructors) for the same. I wanted to allow initiation just
>>> by BaseComponent() that will take the selection and avoid
>>> BaseComponent.some_method as default.
>>>
>>> Goal:
>>> BaseComponent()  # Takes selection
>>> BaseComponent.fromString()  # Takes string name of an object in scene
>>> BaseComponent.fromMObjectHandle()  # Takes an mObjectHandle for an
>>> object in scene
>>> BaseComponent(mObject)  # Takes mObject as default
>>>
>>> Currently trying to implement this with __new__. Previously, all the
>>> "isinstance" checks were done in init which I wanted to simplify.
>>> I couldn't get it to work exactly how I wanted to, but I did get it to
>>> work with all four arg input types (selection, str, mObjectHandle, mObject).
>>>
>>> Problems:
>>> - The problem is there's no clear error/warning if the end-user makes a
>>> mistake.
>>> - All alternative constructors also pass to the init where I had to put
>>> a mObject isinstance check for it to work. If possible, I want to remove
>>> mObject check in init as mObject should be the only one passed to init.
>>>
>>> I have attached my extracted current working code in Pastebin. It has
>>> print statements and can be run in a new Maya session:
>>> https://pastebin.com/tN5bs6g0
>>>
>>> Any suggestions/recommendations are really appreciated. Please let me
>>> know if I wasn't clear enough.
>>> (I have a basic idea about metaclasses and class decorators, so any
>>> advice that includes those is welcome.)
>>>
>>> Thank you!
>>>
>>> --
>>> 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 python_inside_maya+unsubscr...@googlegroups.com.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/python_inside_maya/e97d389f-19c1-49b4-a868-79afafe965fc%40googlegroups.com
>>> <https://groups.google.com/d/msgid/python_inside_maya/e97d389f-19c1-49b4-a868-79afafe965fc%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>> --
> 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 python_inside_maya+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/4451bc9e-ddec-4296-87b9-9b1b12ce41e9%40googlegroups.com
> <https://groups.google.com/d/msgid/python_inside_maya/4451bc9e-ddec-4296-87b9-9b1b12ce41e9%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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 python_inside_maya+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA1a8ebAxcZa-zJZFinxR0cH4BUSC21hx25VeeVymgSHbA%40mail.gmail.com.

Reply via email to