![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
That's the OP's call. He said "short", not "looks short." Abstraction has its uses. As a means of programmer productivity at the expense of performance, I think this one has already failed.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#12 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
Quote:
That said, I think the solution is silly, but for reasons different to your own, DaWei. It seems to me as if this: discount = Percent(20) origprice = 35.00 saleprice = origprice - discount origprice = 35.00 saleprice *= 0.8 Multiplication is well understood by any programmer, and multiplying by 0.8 should instantly be recognisable as a reduction of 20%. Wrapping this up in a class just makes it harder to figure out what's going on. |
|
|
|
|
|
|
#13 |
|
Newbie
Join Date: Aug 2005
Location: Austin, TX
Posts: 15
Rep Power: 0
![]() |
Just a couple more examples:
fica = Percent(7) fedtax = Percent(15) medicare = Percent(3) deductions = fica + fedtax + medicare gross = 100000 net = gross - deductions print net # answer: 75000 wholesale = 10 markup = Percent(35) retail = wholesale + markup print retail # answer: 13.5 yearlyApprec = Percent(8) basis = 10000 newbasis = basis + yearlyApprec + yearlyApprec + yearlyApprec print newbasis # answer: 12597.12 I still think this is rather pretty, the kind of thing that DSL's are made of. Imagine applying this same technique (emulating numeric types using _add_, _radd_, etc.) to applications such as:
Still, here is what the OP started with: Tolerance = 0.05 * Energy Energy = Energy - Tolerance Of course, this is probably the simplest and most direct implementation of subtracting 5% of something from something else. Why was the OP asking the question in the first place? The Percent class illustrates an atypical object capability provided by Python, to be able to define an object that can take additional information from its arithmetic context to modify its behavior. With Percent, the OP can define Tolerance = Percent(5) Energy = Energy - Tolerance Energy -= Tolerance So is this Percent class silly overkill for something so mundane as calculating a percentage? Perhaps. But I find Python's numeric type emulation capabilities to be quite interesting, and it seemed a decent fit and a simple example to the OP's question. Last edited by ptmcg; Jan 31st, 2007 at 6:18 AM. Reason: after re-reading the original post; added one more example |
|
|
|
|
|
#14 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
|
#15 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
Quote:
Unless you consider OO to be more a state of mind, rather than an syntactical attribute of a programming language? |
|
|
|
|
|
|
#16 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I consider OO to be a design methodology that far predates its 'discovery' in the software arena. It is far more than a syntactical attribute in those areas. MEs and hardware designers would probably be surprised to discover that it's considered abstruse.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#17 |
|
Programmer
Join Date: Oct 2005
Posts: 65
Rep Power: 3
![]() |
Thanks, ptmcg, Arevos, DaWei and everyone for your feedback.
As I just started learning Python for work, I was wondering if Python has special utilities or functions that can perform what I posted previously, albeit it is already very short and simple. The examples given are interesting and I thank you all for it. Your different views on OOP (well, from a different perspective, at least) is thought provoking, and it's really beneficial for a fresh graduate like me. Thanks, everyone. |
|
|
|
|
|
#18 |
|
Programmer
|
"%" can be expressed with <b>e-2</b>.
|
|
|
|
|
|
#19 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,799
Rep Power: 5
![]() |
This thread made me smile. Only on a programming forum will you see people carefully rip apart and analyze something as simple as calculating a percentage.
Thanks for the laugh guys. ![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| C++ strange arithmetic? | Mcoy | C++ | 13 | Mar 18th, 2006 12:49 AM |
| Arithmetic | CodeJunkie | Assembly | 2 | Jan 8th, 2006 2:28 PM |
| modulo arithmetic question | LOI Kratong | C++ | 6 | Sep 28th, 2005 2:07 AM |