School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,455 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,595 people online right now. Registration is fast and FREE... Join Now!




Inheritance problem...

 

Inheritance problem..., Why is my custom class not functioning?

code_m

21 Jun, 2009 - 01:58 PM
Post #1

D.I.C Head
**

Joined: 21 Apr, 2009
Posts: 118



Thanked: 8 times
My Contributions
( Python, 3.0 )

I have defined a class called Money_value in which I want to inherit the float class, but whenever I do something that is not defined by the new class, the variable becomes float ?

python

class Money_value(float):
def __str__(self):
"""Return a "beautified" string, to look more like a standard 2-decimal money value
"""
value_list = super().__str__().split(".")
if len(value_list[1]) == 1:
return "$" + value_list[0] + "." + value_list[1] + "0"
else:
cent_list = [x for x in value_list[1]]

# round the second decimal if needed
if len(cent_list) > 2 and int(cent_list[2]) >= 5:
cent_list[1] = str(int(cent_list[1]) + 1)

return "$" + value_list[0] + "." + "".join(cent_list[:2])


so lets say I want to do this:
python

>>> value = Money_value(83.45)
>>> value += 2.30
>>> type(value)
<class 'Money_value'>


I get this:
python

>>> value = Money_value(83.45)
>>> value += 2.30
>>> type(value)
<class 'float'>


I can even do:
python

>>> value = Money_value(83.45)
>>> value += Money_value(2.30)
>>> type(value)
<class 'float'>


I'm so confused as to why any variable continues to silently becomes float?

User is offlineProfile CardPM
+Quote Post


crazyjugglerdrummer

RE: Inheritance Problem...

21 Jun, 2009 - 02:03 PM
Post #2

YOUR AD HERE
Group Icon

Joined: 7 Jan, 2009
Posts: 677



Thanked: 94 times
Dream Kudos: 425
My Contributions
interesting, possibly they're gettting downcast to float since the += is an operator taking a float, unless you extended and implemented it yourself. I haven't heard much about people extending number classes, as normally you want a hasa relationship instead of isa, but do whatever you like . ;D
User is offlineProfile CardPM
+Quote Post

code_m

RE: Inheritance Problem...

22 Jun, 2009 - 06:29 PM
Post #3

D.I.C Head
**

Joined: 21 Apr, 2009
Posts: 118



Thanked: 8 times
My Contributions
I suppose it would be easier to just use a simple function for the str conversion, but I was going for a fully object-oriented approach, and making such a change without inheritance would be outside of this realm.

Oh well.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 01:57AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month