Is this horrible python code?

M

Matthew

Hi,

I am working on a small project as well as trying to learn python. I parse a
text file like

def some_user_func():
# <name> Some user function
#<description> The description
pass

so that 'name', 'description' get put into self.info and the code gets
compilied into a runnable function in self.func. This works but is it
pythonic?
Thanks alot for helping out! matthew.

def print_message():
print "I am a message"

def print_num(num=0):
print 'Number: ', num

class call_me(object):
def __init__(self, func, *args, **kw):
self.func = func
self.args = args
self.kw = kw
def __call__(self, *args, **kw):
print "Execing..."
return self.func(*self.args, **self.kw)

class a_func(object):
def __init__(self, func, info = {}): # name, description, authour, etc
self.func = func
self.info = info
print 'finished a_func.__init__()'
def __call__(self):
print self.info
self.func()
print 'exiting a_func.__call__()'

my_func = a_func(call_me(print_message),{'name': 'Print a message'})
my_func()
another = a_func(call_me(print_num, 42), {'name': 'Print a number'})
another()
 
M

Matthew

Sorry, I should add that the code fragment below is just trying to work out
howto construct the python object to hold the runnable and its information.
To test I fudged what would be the results of parsing in 'my_func' and
'another' in __main__.
----- Original Message -----
From: "Matthew" <[email protected]>
Newsgroups: comp.lang.python
Sent: Thursday, November 06, 2003 8:47 AM
Subject: Is this horrible python code?
 
C

Christian Seberino

I'd be happy to help except I cannot decipher what you are asking.

Chris
 
M

Michael George Lerner

Matthew said:
class call_me(object):
def __init__(self, func, *args, **kw):
self.func = func
self.args = args
self.kw = kw
def __call__(self, *args, **kw):
print "Execing..."
return self.func(*self.args, **self.kw)

I'm not sure what you're trying to do with the rest of this, but
I think that your call_me class isn't doing what you want.

asteroids% python
Python 2.2 (#1, Feb 18 2002, 14:48:51) [C] on sunos5
Type "help", "copyright", "credits" or "license" for more information. .... def __init__(self,a):
.... self.a = a
.... def __call__(self,a):
.... print "self.a is '%s' and a is '%s'" % (self.a,a)
....
so, i don't think your __call__ method will do what you
want. then again, i'm not exactly sure what you're trying to
do, so i can't figure out why you want to initialize things
your objects with func, args and kw *and* want to be able
to specify args and kw at call time.

-michael
 
M

MetalOne

I think it is simpler using closures.

def auditFunc(info, func, *args, **kw):
def inner():
print 'finished a_func.__init__()'
print info
print "Execing..."
ret = func(*args, **kw)
print 'exiting a_func.__call__()'
return ret

return inner

f = auditFunc({'name': 'Print a message'}, print_message)
f()
another = auditFunc({'name': 'Print a number'}, print_num, 42)
another()
 

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
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top