469,336 Members | 5,442 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

Defining class methods outside of classes

Hi, I'm working on a bot written in python. It will consist of a
mostly empty class that will then call a loader which in turn defines
functions and adds them to the class. At the moment, I do this by
using execfile(file,globals()) and calling a load(bot) method defined
in every "module" which takes the functions defined in that perticular
module and does bot.function=function. The problem with that is when I
call bot.function() self doesn't get passed as an argument to the
function. Is there anything I can do to sort this besides calling
bot.function(bot, ...) everytime?

--
Lord Landon rules over all!
May 3 '06 #1
1 1364
Lord Landon wrote:
Hi, I'm working on a bot written in python. It will consist of a
mostly empty class that will then call a loader which in turn defines
functions and adds them to the class. At the moment, I do this by
using execfile(file,globals()) and calling a load(bot) method defined
in every "module" which takes the functions defined in that perticular
module and does bot.function=function. The problem with that is when I
call bot.function() self doesn't get passed as an argument to the
function. Is there anything I can do to sort this besides calling
bot.function(bot, ...) everytime?


Either add the method to the class or make it an instance method.
class Bot(object): .... def __init__(self):
.... self.name = "bot"
.... bot = Bot()
def method(self): print "hello from", self.name .... Bot.greet = method
bot.greet() hello from bot import new
bot.hello = new.instancemethod(method, bot)
bot.hello()

hello from bot

Peter

May 4 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Harry Pehkonen | last post: by
7 posts views Thread by Kerry Neilson | last post: by
42 posts views Thread by Dan | last post: by
6 posts views Thread by JSheble | last post: by
26 posts views Thread by Cliff Williams | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by haryvincent176 | last post: by
reply views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.