Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Defining class methods outside of classes

Reply
Thread Tools

Defining class methods outside of classes

 
 
Lord Landon
Guest
Posts: n/a
 
      05-03-2006
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!
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      05-04-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
help with pix inside->outside + dmz->outside + inside->outside->dmz Jack Cisco 0 09-19-2007 01:57 AM
Use of Static Variables from Outside of Defining Class Chris Portka C++ 3 12-06-2006 02:23 AM
defining classes-- different methods Cliff Williams Javascript 25 11-29-2006 09:41 AM
Defining methods outside of script tags Jasper Pearlman ASP .Net 3 08-16-2004 12:26 PM



Advertisments