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.hars...@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/CAPaTLMQ70FCh4JNBeY4fRL22ByR-%3DgGkE8AaGegWo8OMTfU05g%40mail.gmail.com.

Reply via email to