User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 426,811 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,921 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 222 | Replies: 1
Reply
Join Date: Jul 2008
Posts: 2
Reputation: mayapower is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mayapower mayapower is offline Offline
Newbie Poster

subtype custom 'float'. How to?

  #1  
Jul 17th, 2008
Hi,

I am trying to create a cutom 'Float' type using built in 'float' type. Here is the code:
  1. class Attribute(object):
  2. """
  3. Common attributes and methods for custom types
  4. """
  5.  
  6. def __init__(self, name=None, type=None, range=None):
  7.  
  8. self.__name = name
  9. self.__type = type
  10. self.__range = range
  11.  
  12. #Read only attributes
  13. name = property(lambda self: self.__name)
  14. type = property(lambda self: self.__type)
  15. range = property(lambda self: self.__range)
  16.  
  17.  
  18. class Float(float, Attribute):
  19. '''Custom Float type'''
  20. __slots__ = ('__name', '__type', '__range')
  21. def __init__(self, value=0.0, name=None, type=None, range=(0.0, 1.0)):
  22. try:
  23. super(Float, self).__init__(name=name, type=type, range=range)
  24. float.__init__(self, value)
  25. except:
  26. print 'Error : %s %s' % sys.exc_info()[:2]
  27.  
  28. a = Float(2.0, name='myFloat')

The problem is, I am not able to pass arguments when creating an instance. How do I solve this?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2006
Posts: 562
Reputation: jrcagle is on a distinguished road 
Rep Power: 4
Solved Threads: 72
jrcagle jrcagle is offline Offline
Posting Pro

Re: subtype custom 'float'. How to?

  #2  
Jul 17th, 2008
I would probably take a completely different approach:

  1. class MyFloat(float):
  2.  
  3. def __new__(self, name, value):
  4. return float.__new__(self, value)
  5.  
  6. def __init__(self, name, value):
  7. self._name = name
  8.  
  9. name = property(lambda self: self._name)
  10.  
  11. x=MyFloat("name", 1.5)
  12. print x
  13. print x._name
  14. print x.name

The call to __new__ is necessary when subclassing immutable types like float -- the call order is __new__, followed by __init__ on initialization, and all of the arguments are passed to both.

Jeff
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the Python Forum

All times are GMT -4. The time now is 8:01 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC