Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 1st, 2004, 05:57 PM
antarctic antarctic is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 17 antarctic User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
list of functions

This may seem like an odd requirement, but I would like to declare a class with one of its methods definable at instantiation. So that for instance I might have the following

Code:
class Func:
    numargs = 0
    rettype = None
    function = None
    def __init__(self, numargs, rettype, function):
        self.numargs = numargs
        self.rettype = rettype
        self.function = function

cos = Func(1,'float', 'return math.cos(arg1)')
sin = Func(1,'float', 'return math.sin(arg1)')

print cos.function(0)   # this should output 1


Does anyone know any way to make something like this work? The reason I need the class to be like this is that I want to maintain a list of functions along with certain characteristics and be able to call each one. I would also like to be able to dynamically add to the list. I could hardcode all the functions into the program and hardcode a list of the functions with parameters but that does not allow me to add functions on the fly. Any pointers would be greatly appreciated

Reply With Quote
  #2  
Old November 2nd, 2004, 04:47 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 11 m 13 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
I've tried to stay as close to your example as possible in this example but usually you would use __setattr__ method for this kind of thing; this lets you do things like f.x = value

Code:
>>> class Foo: 
...     def __init__(self, *args):   
...         for each in args:
...             setattr(self, each.func_name, each)
... 
>>> def foo(x):
...     return x
... 
>>> def bar():
...     print 'Bar!!!'
... 
>>> f = Foo(foo, bar)
>>> f.foo(True)
True
>>> f.bar()
Bar!!!
>>> 


Sorted . As you can see, you can now add functions to your class instance when you initiate it.

You can find a simple example of using __setattr__ in the Python Cookbook: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65207

Hope this helps,

Mark.
__________________
programming language development: www.netytan.com Hula


Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > list of functions


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT