473,322 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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 1458
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Harry Pehkonen | last post by:
I have been defining new class methods when I'm trying to simplify some code. But I'm thinking I should just define functions within that method because they aren't useful from the outside anyway....
7
by: Kerry Neilson | last post by:
Hi, Really hung up on this one. I'm trying to get all the fields of a dictionary to be unique for each class: class A { my_dict = dict_entry = { 'key1':0, 'key2':0 } __init__(self): for...
3
by: Robert | last post by:
Python doesn't know the class of a method when container not direct class attribute: >>> class X: .... def f():pass .... g=f .... l= .... >>> X.g <unbound method X.f>
42
by: Dan | last post by:
Hello, I have trouble with class calling. I am calling getvolume() with succes in the function CreateCircle but it do not want to call it in ShowCircle() function. I am staying in the same...
6
by: JSheble | last post by:
I come from a Delphi background, and am currently trying to port or convert some of our Delphi classes to C#. I've got a good handle on basic class design, but am a bit lost with some of the more...
9
by: MariusI | last post by:
Consider the following class layout public class Order { public ProductOrder AddProductOrder(/* variables required to create a product order */) { /* Check if the product order can be added...
7
by: WXS | last post by:
Vote for this idea if you like it here: http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=5fee280d-085e-4fe2-af35-254fbbe96ee9...
26
by: Cliff Williams | last post by:
Can someone explain the pros/cons of these different ways of creating a class? // 1 function myclass() { this.foo1 = function() {...} } // 2a
11
by: Aflj | last post by:
This code won't compile (two compilers tried, gcc and VC++, both of recent versions, but I don't remember them exactly): class C1 { public: void M1(int i) {} }; class C2: public C1
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.