Is this horrible python code?

Matthew matthew at newsgroups.com
Wed Nov 5 15:03:03 EST 2003


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" <matthew at newsgroups.com>
Newsgroups: comp.lang.python
Sent: Thursday, November 06, 2003 8:47 AM
Subject: Is this horrible python code?



"Matthew" <matthew at newsgroups.com> wrote in message
news:bobjp2$u73$1 at lust.ihug.co.nz...
> 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