No problem. Metaclasses are confusing, at best.
It's basically an exact copy of UndefinedFunction in
sympy/core/function.py. Assuming that OneQuibitGate is an instance of
Basic (so that its metaclass is ManagedProperties), just have
from sympy.core.core import BasicMeta
from sympy.core.assumptions import ManagedProperties
class CreateOneQuibitGate(ManagedProperties):
def __new__(mcl, name):
return BasicMeta.__new__(mcl, name + "Gate", (OneQubitGate,),
{'gate_name': name, 'gate_name_latex': name})
Don't worry about the __module__ = None thing from UndefinedFunction;
that's there to make it picklable in certain situations, but it
doesn't really even work. You could probably get away with using
BasicMeta instead of ManagedProperties, but it's probably a bad idea
to use a different metaclass than the rest of the core.
You can of course get more clever with the gate_name and
gate_name_latex. I just used the same name for all, but you might want
to have two or three arguments in the constructor.
It works like this:
In [20]: a = CreateOneQuibitGate('V')
In [21]: a.gate_name
Out[21]: 'V'
In [22]: a()
Out[22]:
V
O
Aaron Meurer
On Wed, Jul 24, 2013 at 9:14 AM, Rick Muller <[email protected]> wrote:
> Aaron,
>
> I'm being dense. Can you point me to some tips on how to use this? Say I
> want to define something like
>
> class VGate(OneQubitGate):
> gate_name = 'V'
> gate_name_latex = u'V'
>
> on the fly (e.g. so that the 'V' is defined) how do I do this with
> UndefinedFunction or BasicMeta or some other metaclass in function.py?
>
> (Not asking you to do it, just point me to some examples or something).
>
> On Thursday, July 18, 2013 10:27:04 AM UTC-6, Aaron Meurer wrote:
>>
>> Take a look at how undefined functions (like Function('f')) work in
>> sympy/core/function.py (look at UndefinedFunction and all its super
>> classes and metaclasses). They are basically dynamically created
>> classes.
>>
>> Aaron Meurer
>>
>> On Thu, Jul 18, 2013 at 11:22 AM, Rick Muller <[email protected]> wrote:
>> > We could do some metaprogramming like they do to define namedtuple, but
>> > I
>> > don't know whether hacks like that are looked down upon in sympy.
>> >
>> >
>> > On Thursday, July 18, 2013 10:20:06 AM UTC-6, Rick Muller wrote:
>> >>
>> >> But I don't know how do define a function that does this, without
>> >> defining
>> >> arguments or
>> >
>> >
>> >>
>> >> modifying the object after creation, both of which I understand are
>> >> forbidden in sympy operators.
>> >>
>> >>
>> >> Any thoughts?
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups
>> > "sympy" group.
>> > To unsubscribe from this group and stop receiving emails from it, send
>> > an
>> > email to [email protected].
>> > To post to this group, send email to [email protected].
>> > Visit this group at http://groups.google.com/group/sympy.
>> > For more options, visit https://groups.google.com/groups/opt_out.
>> >
>> >
>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/sympy.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
--
You received this message because you are subscribed to the Google Groups
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sympy.
For more options, visit https://groups.google.com/groups/opt_out.