•
•
•
•
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
![]() |
•
•
Join Date: Jul 2008
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
Hi,
I am trying to create a cutom 'Float' type using built in 'float' type. Here is the code:
The problem is, I am not able to pass arguments when creating an instance. How do I solve this?
I am trying to create a cutom 'Float' type using built in 'float' type. Here is the code:
python Syntax (Toggle Plain Text)
class Attribute(object): """ Common attributes and methods for custom types """ def __init__(self, name=None, type=None, range=None): self.__name = name self.__type = type self.__range = range #Read only attributes name = property(lambda self: self.__name) type = property(lambda self: self.__type) range = property(lambda self: self.__range) class Float(float, Attribute): '''Custom Float type''' __slots__ = ('__name', '__type', '__range') def __init__(self, value=0.0, name=None, type=None, range=(0.0, 1.0)): try: super(Float, self).__init__(name=name, type=type, range=range) float.__init__(self, value) except: print 'Error : %s %s' % sys.exc_info()[:2] 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?
•
•
Join Date: Jul 2006
Posts: 562
Reputation:
Rep Power: 4
Solved Threads: 72
I would probably take a completely different approach:
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
Python Syntax (Toggle Plain Text)
class MyFloat(float): def __new__(self, name, value): return float.__new__(self, value) def __init__(self, name, value): self._name = name name = property(lambda self: self._name) x=MyFloat("name", 1.5) print x print x._name 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
![]() |
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Other Threads in the Python Forum
- Previous Thread: Need help with confusing issue !!
- Next Thread: Reading of Excel


Linear Mode