Referencing objects own functions

P

pip

Hi all,

Pretend I have the following:

class foo:
commands = [
'name': 'help',
'desc': 'Print help',
]

def print_help(self):
print "Help is on the way!"

I would like to add another entry to the commands dict. called 'func'
or something which contains a reference to a function in the foo
class. What would the entry look like if atall possible?

I would rather not use eval if possible.

Thanks,

Pip
 
P

Peter Otten

pip said:
Hi all,

Pretend I have the following:

class foo:
commands = [
'name': 'help',
'desc': 'Print help',
]

def print_help(self):
print "Help is on the way!"

I would like to add another entry to the commands dict. called 'func'
or something which contains a reference to a function in the foo
class. What would the entry look like if atall possible?

I would rather not use eval if possible.

As a rule of thumb, you can do everything in a class suite that can be done
in a function definition.

def print_help(self):
# code

binds the print_help name to a function object in the same way as
one = 1 binds 'one' to the integer object 1. Therefore you have to define it
before you can use the reference:
.... def print_help(self):
.... print "help is on the way"
.... commands = dict(
.... name="help",
.... desc="print help",
.... func=print_help)
....
There are two ways to call commands["func"], but both require that you
explicitly provide the self parameter:
fooinst.commands["func"](fooinst) help is on the way
foo.commands["func"](fooinst)
help is on the way

While calling the function stored in the commands dictionary is a bit more
tedious than normal method invocation, all efforts to simply that depend
heavily on what you actually want to do with the function stored in the
dictionary.


Peter
 
A

Alex Martelli

pip said:
Hi all,

Pretend I have the following:

class foo:
commands = [
'name': 'help',
'desc': 'Print help',
]

def print_help(self):
print "Help is on the way!"

I would like to add another entry to the commands dict. called 'func'
or something which contains a reference to a function in the foo
class. What would the entry look like if atall possible?

I would rather not use eval if possible.

Wise (eval only in case of dire need:). I would use the name of the
method rather than the actual function object, for two reasons:
1. you can keep the commands dict BEFORE the def;
2. you can getattr and have the magic of descriptors work for you.

class foo:
commands = dict(
name='help',
desc='Print help',
func='print_help",
)

def print_help(self):
print "Help is on the way!"

given an f=foo(), to call the 'func' you just need:

getattr(f, f.commands['func'])()

to get the same effect as f.print_help() would. It doesn't get much
simpler, unless you instrument class foo (e.g. by inheriting from some
appropriate base class which wraps such operations into a nice readable
method).


Alex
 
P

pip

Alex,

pip said:
[...]
I would like to add another entry to the commands dict. called 'func'
or something which contains a reference to a function in the foo
class. What would the entry look like if atall possible?

I would rather not use eval if possible.
[...]
getattr(f, f.commands['func'])()

Just what I was looking for.

Thanks all for your help,

Pip
 

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,209
Messages
2,571,088
Members
47,686
Latest member
scamivo

Latest Threads

Top