473,385 Members | 1,409 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,385 software developers and data experts.

Create static method dynamically

Hello,

Here is my problem :

class myClass:
def addmethod(name,method):
maClasse.__dict__[name] = method
addmethod = staticmethod(addmethod)

def hello():
print "hello"

myClasse.addmethod('hello',hello)
Traceback (most recent call last):
File "<pyshell#0>", line 1, in -toplevel-
myClass.hello()
TypeError: unbound method hello() must be called with myClass instance
as first argument (got nothing instead)

I would like to make this call:
myClass.hello()

Is there a way of making 'hello' a
static method of 'myClass ?

Regards

Jul 18 '05 #1
4 1641
Salvatore wrote:
def hello():
print "hello" Is there a way of making 'hello' a
static method of 'myClass ?

class Test: .... def addStaticMethod(name, func):
.... setattr(Test, name, staticmethod(func))
.... addStaticMethod = staticmethod(addStaticMethod)
.... def hello(): print "hello" .... t = Test()
t.addStaticMethod("hello", hello)
t.hello() hello
Even simpler, if you know the method name beforehand:
Test.world = staticmethod(hello)
t.world()

hello

Peter
Jul 18 '05 #2
As I always say, the beauty is in the simplicity :-)
Thank you very much Peter

Regards

Salvatore

Jul 18 '05 #3
Salvatore wrote:
class myClass:
def addmethod(name,method):
maClasse.__dict__[name] = method
addmethod = staticmethod(addmethod)

def hello():
print "hello"

myClasse.addmethod('hello',hello) <snip> I would like to make this call:
myClass.hello()


Change line 3 to:
myClass.__dict__[name] = staticmethod(method)
Jul 18 '05 #4
Leif K-Brooks wrote:

Change line 3 to:
myClass.__dict__[name] = staticmethod(method)


That make me feel not very proud ;-)
Thans Leif

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: john smith | last post by:
I'm wondering if it's possible to declare a pure virtual member function? Ie is: class A{ public: virtual static void f() const = 0; }; legal? I'm getting compile errors for code that used...
5
by: Chris | last post by:
Hi I have a scenario where I've created another AppDomain to dynamically load a DLL(s) into. In this newly loaded DLL I want to call a static method on a class. The problem arise is that I have...
3
by: Kiyomi | last post by:
Hello, I create a Table1 dynamically at run time, and at the same time, I would like to create LinkButton controls, also dynamically, and insert them into each line in my Table1. I would...
4
by: Ray | last post by:
I want to dynamically load DLLs (created from VB) and instantiate a class with a particular name, like "ProcessClass". I am able to load the DLL and confirm there is a class by that name BUT I...
4
by: vertigo | last post by:
Hello I need to create some objects durring program execution - but it's names are dynamically generated (depends on parameters). How can i do it ? Thanx Michal
2
by: macca | last post by:
Hi, I am writing a GUI application. It will have a number of user defined controls( I plan to use/create a user defined control that will output alarm states that the user can also select and...
6
by: James Norton-Jones | last post by:
Hi, I am wanting to create Linkbuttons and Event Handlers on the fly. Ideally I would be able to pass the CommandName and CommandArgument to the Event Handler which in turn would pass these to...
44
by: John A. Bailo | last post by:
Dr. Dobbs has a /glowing/ article on Ruby on Rails this month. What do you guys think? Can it replace .net, php and java? And be the Open Source OOP web solution that is not bound to Sun or...
4
by: Benny Dein | last post by:
Hi I want to create a servlet or something similar by which im able to create virtual webservices. Lets say i would like to create a webservice with a method called 'getDate' which returnes...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.