Then the event instance could be created with arbitrary data attached,
evt = SomeNewEvent(attr1="hello", attr2=654)
But I don't understand why the constructing of SomeNewEvent(attr1=..., attr2=...) could be valid from the language's point of view. Does it mean that SomeNewEvent class automatically have members 'attr1' and 'attr2'? How did it happen? I think I lost with some knowledge of the Python language. Could someone please explain to me?
Then the event instance could be created with arbitrary data attached,
evt = SomeNewEvent(attr1="hello", attr2=654)
But I don't understand why the constructing of SomeNewEvent(attr1=...,
attr2=...) could be valid from the language's point of view. Does it mean
that SomeNewEvent class automatically have members 'attr1' and 'attr2'? How
did it happen? I think I lost with some knowledge of the Python language.
Could someone please explain to me?
Thanks in advance.
Everything is an object. The method simply returns a class (not a
class instance) that is assigned to whatever identifier you have given
it (SomeNewEvent).
The source code is on your system, just open the newevent module in
the wx/lib folder of your installation to see how it works.
def NewEvent():
"""Generate new (Event, Binder) tuple
e.g. MooEvent, EVT_MOO = NewEvent()
"""
evttype = wx.NewEventType()
class _Event(wx.PyEvent):
def __init__(self, **kw):
wx.PyEvent.__init__(self)
self.SetEventType(evttype)
self.__dict__.update(kw)
return _Event, wx.PyEventBinder(evttype)
The magic happens in the __init__ method. The **kw parameter tells python to put all the extra keyword args that do not match an actual parameter into a dictionary called kw. Then the last line loads those key/value pairs into self.__dict__, which is where Python stores all the attributes of the class instance. After that those values are accessible in self just like normal.
···
On 5/28/12 10:24 PM, narke wrote:
Hi,
With wx.lib.newevent, one can create a custom event class such as,
Then the event instance could be created with arbitrary data attached,
evt = SomeNewEvent(attr1="hello", attr2=654)
But I don't understand why the constructing of SomeNewEvent(attr1=...,
attr2=...) could be valid from the language's point of view. Does it
mean that SomeNewEvent class automatically have members 'attr1' and
'attr2'? How did it happen? I think I lost with some knowledge of the
Python language. Could someone please explain to me?
Then the event instance could be created with arbitrary data attached,
evt = SomeNewEvent(attr1="hello", attr2=654)
But I don't understand why the constructing of SomeNewEvent(attr1=...,
attr2=...) could be valid from the language's point of view. Does it
mean that SomeNewEvent class automatically have members 'attr1' and
'attr2'? How did it happen? I think I lost with some knowledge of the
Python language. Could someone please explain to me?
Thanks in advance.
def NewEvent():
"""Generate new (Event, Binder) tuple
e.g. MooEvent, EVT_MOO = NewEvent()
"""
evttype = wx.NewEventType()
class _Event(wx.PyEvent):
def __init__(self, **kw):
wx.PyEvent.__init__(self)
self.SetEventType(evttype)
self.__dict__.update(kw)
return _Event, wx.PyEventBinder(evttype)
The magic happens in the __init__ method. The **kw parameter tells
python to put all the extra keyword args that do not match an actual
parameter into a dictionary called kw. Then the last line loads those
key/value pairs into self.__dict__, which is where Python stores all the
attributes of the class instance. After that those values are accessible
in self just like normal.