how to make a code object a function

  • Thread starter Diez B. Roggisch
  • Start date
D

Diez B. Roggisch

Hi,

I've got code objects that are created from strings at runtime that look
like this:

def p_op1(_, args):
_.add_dependency('p_op1')

As you migth guess, p_op1 is supposed to become an instance method. Now how
do I make this code object a function-object that can be set into the
instances __dict__ to make it a callable method?

Thanks for any advice,

Diez B. Roggisch
 
P

Peter Otten

Diez said:
Hi,

I've got code objects that are created from strings at runtime that look
like this:

def p_op1(_, args):
_.add_dependency('p_op1')

As you migth guess, p_op1 is supposed to become an instance method. Now
how do I make this code object a function-object that can be set into the
instances __dict__ to make it a callable method?

Too lazy to type self?
.... def add_dependency(self, name):
.... print "adding dependency", name
........ _.add_dependency("p_op1")
....
By the way, types.MethodType and new.instancemethod seem to be equivalent.

Peter
 
D

Diez B. Roggisch

Hi,
Too lazy to type self?

yes. I used an underscore in java as indication for member-variables, so
typing _. appeared natural to me.
... def add_dependency(self, name):
... print "adding dependency", name
...
... _.add_dependency("p_op1")
...

By the way, types.MethodType and new.instancemethod seem to be equivalent.

Interesting - from the docs, I can't see that types.MethodType has a
constructor like this.

Your example above gave me the idea to simply exec() my code-object, so in
the end I have a new function called p_op1 in my scope - that function I
can get using locals()['p_op1']. I know the name, so that works.

Just out of curiosity - is there a way to know the name of a code object you
know nothing about except that it will become a function definition? I
guess I could go for some AST-stuff looking for a "def foo" statement, so I
know I will end up having defined foo when exec'ing the code object.

Regards,

Diez B. Roggisch
 
E

Emile van Sebille

Diez B. Roggisch:
Just out of curiosity - is there a way to know the name of a code object you
know nothing about except that it will become a function definition? I
guess I could go for some AST-stuff looking for a "def foo" statement, so I
know I will end up having defined foo when exec'ing the code object.

I'm not quit sure what you're asking, but does sets help?
Set(['testing'])


Emile van Sebille
(e-mail address removed)
 
P

Peter Otten

Diez said:
Interesting - from the docs, I can't see that types.MethodType has a
constructor like this.

It get's really funny if you look into the source (types.py):

class _C:
def _m(self): pass
ClassType = type(_C)
UnboundMethodType = type(_C._m) # Same as MethodType
_x = _C()
InstanceType = type(_x)
MethodType = type(_x._m)

Seems we caught them cheating here :)
Just out of curiosity - is there a way to know the name of a code object
you know nothing about except that it will become a function definition? I
guess I could go for some AST-stuff looking for a "def foo" statement, so
I know I will end up having defined foo when exec'ing the code object.

You could provide a separate namespace and then extract only the callables:
d = {}
exec "factor=2\ndef alpha(s, t): print factor*t" in d
d.keys() ['__builtins__', 'alpha', 'factor']
funcs = dict([(n,f) for n, f in d.iteritems() if callable(f)])
funcs
{'alpha': said:
funcs["alpha"](None, 2)
4

Peter
 
D

Diez B. Roggisch

You could provide a separate namespace and then extract only the
callables:
d = {}
exec "factor=2\ndef alpha(s, t): print factor*t" in d
d.keys() ['__builtins__', 'alpha', 'factor']
funcs = dict([(n,f) for n, f in d.iteritems() if callable(f)])
funcs
{'alpha': said:
funcs["alpha"](None, 2)
4

And again I learned something completely new - cool. Thanks,

Diez
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,174
Messages
2,570,940
Members
47,486
Latest member
websterztechnologies01

Latest Threads

Top