What is Python good for?

brueckd at tbye.com brueckd at tbye.com
Thu Sep 13 10:45:15 EDT 2001


On Thu, 13 Sep 2001, maxm wrote:

> "Ignacio Vazquez-Abrams" <ignacio at openservices.net> wrote in message
> > Here's something that came up recently that if it doesn't make you go
> > "ooh-ahh", then it will make you go "whoa":
>
> > >>> def a():
> > ...   return 'abc'
> > ...
> > >>> def b():
> > ...   return 123
> > ...
>
> > >>> a.func_code=b.func_code
>
> wouldn't a = b satisfy your need for whoas here ?
>
> > >>> a()
> > 123
>
> regards Max M

It's the implications of what Ignacio is doing that causes the 'whoa'
response:

import marshal, socket, threading, time
def listen():
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  s.bind(('',5555)
  s.listen(1)
  while 1:
    q,v = s.accept()
    print 'Got connection'
    def bar(): pass
    bar.func_code = marshal.loads(q.recv(4096))
    print 'Executing function'
    bar()

threading.Thread(target=listen).start()

def foo():
  print 'foo'

code = marshal.dumps(foo.func_code)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('',5555)
s.send(code)
time.sleep(5.0)

Cool!
-Dave





More information about the Python-list mailing list