inelegance in calling dynamically created class methods from scripting level

D

Danny Shevitz

Howdy,

I am trying to call class methods that have been created via a "type"
function. I have enclosed a simplified example that shows what I am trying
to do. In particular I am calling from the scripting level which is in a
different namespace than the dynamically created class. I can call the
method, but only by passing strings and using getattr.I find this inelegant.
I would rather pass a callable object, but cannot figure out how to do this.
So in the example below, I'm trying to get a variant of "myApply" to work.
"myApply2" works but uses strings.

thanks,
Danny

#---%<-------------------------------------------------------------------
# The purpose of this test is to test ways
# to call subclass methods from a scripting level

class Item(object):
def __init__(self,val):
self.value=val

def myPrint(self):
print self.value

class DoSomething(object):
def __init__(self, dict):
ItemChild = type('Item_child',(Item,),dict)
self.childClass=ItemChild
# create an example list
self.itemList = []
for i in range(5):
self.itemList.append(ItemChild(i))

# can't get to work with callable objects
def myApply(self, whatever):
for item in self.itemList:
whatever(item)

# works with string arguments
def myApply2(self, whatever):
func = getattr(self.childClass,whatever)
for item in self.itemList:
func(item)

##########################################################
# begin scripting code
##########################################################

# child method to be spliced in
def reset(self,val=0):
self.value=val

# construct a DoSomething class
ds = DoSomething({'hoopty':reset})

# I am trying to get something like this working...
# ds.myApply(???something callable???) Can't get to work!

# control operation from the scripting level
ds.myApply2('myPrint')
ds.myApply2('hoopty')
ds.myApply2('myPrint')
 
A

Andrew Durdin

# ds.myApply(???something callable???) Can't get to work!

You need to be able to reference the callable via some name in the
current scope. For 'myPrint' that's easy:
ds.myApply(Item.myPrint)

For 'hoopty', you can't do that, because in the current scope you
don't have a name bound to any object with the 'hoopty' attribute. But
since you know that the hoopty attribute was bound to the reset
function, you can get the desired effect like this:
ds.myApply(reset)
 
M

Marc Christiansen

Andrew Durdin said:
You need to be able to reference the callable via some name in the
current scope. For 'myPrint' that's easy:
ds.myApply(Item.myPrint)

For 'hoopty', you can't do that, because in the current scope you
don't have a name bound to any object with the 'hoopty' attribute. But
since you know that the hoopty attribute was bound to the reset
function, you can get the desired effect like this:
ds.myApply(reset)

Well, one could use
ds.myApply(ds.childClass.hoopty)

but this looks strange to me. It asks me: "Do I really want this
approach?".

Marc
 
D

danny

thanks for your help. Your code works. I guess the realization is
that fun(self,*args) really is identical to self.fun(*args). An
ordinary function called with the former syntax still works even if it
really isn't an instance method.

Danny
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top