up with PyGUI!
Bengt Richter
bokr at oz.net
Sun Sep 26 17:39:10 EDT 2004
On 26 Sep 2004 05:35:03 GMT, "OKB (not okblacke)" <brenNOSPAMbarn at NObrenSPAMbarn.net> wrote:
>Bengt Richter wrote:
>
>> myFrame = (
>> Frame(
>> Sizer(
>> CustButton(size= ... etc),
>> CustButton(size= ... etc),
>> Panel(
>> Sizer(
>> CustListBox(etc),
>> CustListBox(etc),
>> CustButton(etc),
>> Text( etc )
>> )
>> )
>> )
>> )
>> )
>
> One problem with this is that it's not possible to define methods
>on your widgets in this kind of structure.
^^-- I take it you mean within...
Hm, yes. Need a full lambda or "anonymous def" to do that inline. At least
for non-trivial methods. Wasn't thinking about that. Hm, "not possible" pushes
a button somewhere deep in my psyche though. I guess it's the occasional reward of
discovery that keeps the circuit alive ;-) Not sure ... ;-)
>
>>>If Python's syntax were more flexible, this could perhaps be done
>>>in other ways -- I'm not saying I want that -- but as it is the
>>>only real way to construct nested structures in Python is with
>>>class definitions.
>> You don't mean that ("...only real way...") as broadly as it
>> sounds, do you?
>
> Sorry, you're right. Obviously dictionaries and lists can be
>nested. A key point, though, is the one I mentioned above: classes are
>the only structure which allow you define nested structures with
>arbitrary in-line code. The "in-line" proviso there may seem obtuse,
>but I think it's important. For instance, if you define a button, you
>should be able to define the event handler for a click on that button
>right there, as part of writing the button code. Having to separate the
>event-handling code out into a separate routine leads to
>spaghettification, and also introduces a bunch of naming problems (you
>need to have a consistent naming convention that links a widget to its
>event handler).
whatever = factory("""
any source format you like # ;-)
""")
Not entirely kidding.
>
> (Also, just for the record: I know you can nest function
>definitions in other functions, but this isn't useful here because
I was going to chase down that rabbit trail a bit ;-)
>there's no way to access the nested function definition from outside the
There you go again, pushing the "no way" button ;-)
>enclosing function, whereas you can reference nested classes with normal
>attribute access.)
The trouble with accessing nested functions is there's no unboxed way to mutate
closure variables, otherwise I think you could get around the "no way" building
on something like
>>> def nester(*args, **kw):
... def foo(*a,**k): print 'foo here with', args, a, k
... def bar(*a,**k): print 'bar here with', args, a, k
... def accessnest(self, name, *args, **kw):
... return getattr(self, name)(*args, **kw)
... accessnest.foo = foo
... accessnest.bar = bar
... accessnest.__dict__.update(kw)
... return accessnest.__get__(accessnest)
...
>>> callnested = nester('nester arg', outsider=lambda x: 2*x)
>>> callnested('foo', 1,2, hi='ho')
foo here with ('nester arg',) (1, 2) {'hi': 'ho'}
>>> callnested('bar')
bar here with ('nester arg',) () {}
>>> callnested('outsider', ' xxx')
' xxx xxx'
>>> callnested('outsider', 123)
246
Not that this is crisp and clean and lean as a way of expressing design intent ;-)
Regards,
Bengt Richter
More information about the Python-list
mailing list