Is this horrible python code?

Matthew matthew at newsgroups.com
Wed Nov 5 14:47:50 EST 2003


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()






More information about the Python-list mailing list