c[:]()
Warren Stringer
warren at muse.com
Thu May 31 21:42:05 EDT 2007
> > I did not hijack another thread
>
> You really did. In the first message you sent, we see the following
> header:
>
> > In-Reply-To: <1180504773.374529.161740 at q66g2000hsg.googlegroups.com>
...
Damn! I suck. Outlook as a newsreader sucks. I need to use something else.
> I retyped the code you posted in the first post, and did not get the
> same results as you. Specifically:
> >>> def a: print 'a'
> SyntaxError: invalid syntax
> >>> def b: print 'b'
> SyntaxError: invalid syntax
> those obviously were not copied from working code.
>
> >>> c[:]()
> TypeError: 'tuple' object is not callable
>
> this has the correct spelling of 'tuple' in it. Your post misspelled it.
>
> and finally,
> >>> c[0]()
> a
> >>> c[:][0]
> <function a at 0x00CBB130>
>
> I don't know how you could have gotten c[:][0] to print 'a', but it
> wasn't by running all of the code you presented to us.
>
> --
> Jerry
They were copied from working code. Copied *badly*? Yes. Running python via:
Windows -> start -> run -> python
doesn't allow cut and paste
Here is what I tested, now cut and past from visual studio IDE
#--------------------
def a(): print 'a'
def b(): print 'b'
c = (a,b)
c[:]() # typeError
c[0]() # expected
c[:][0]() # huh?
[i() for i in c] # too long and ...huh?
#--------------------------------------
Now, I'm extremely annoyed at my own original post, for misleading typos.
Yeah, Mikael's example is really cool! And I followed his suggestion of
making it cellphone friendly by shorting the name, like so:
#--------------------------------------
class do(list):
def __call__(self,*args,**kwargs):
return [f(*args,**kwargs) for f in self]
def a(): print 'a called'
def b(): print 'b called'
c = do()
c = [a,b]
do(c[:])()
do(c)()
#--------------------------------------
I prefer that last line, because [:] is very expensive to type from a cell
phone.
In summary:
Sorry about the hijacked thread
Sorry about the typos
Outlook is an odd newsreader
Mikael's solution works
No PEP needed for this example
Now, if I could only could do this:
do(orchestra(conductor)).play()
Cheers,
\~/
More information about the Python-list
mailing list