How about adding rational fraction to Python?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Thu Feb 28 23:33:48 CET 2008


On Thu, 28 Feb 2008 11:13:27 -0500, D'Arcy J.M. Cain wrote:

>> > 	Automatic conversions, okay... but converting a result when all
>> > inputs are of one time, NO...
>> 
>> What? How does that make any sense?
>> 
>> By that logic, we should see this:
>> 
>> >>> len("a string")
>> '8'
>> >>> len([2, 4, 6])
>> [3]
>> >>> len({'key': 'value'})
>> {1: None}
> 
> I think that you have to show your work here.  How does the above
> statement about operators imply that the len method should return the
> type of its argument?


Consider the argument list to the function call len("a string").

>>> args = ["a string"]  # all the arguments
>>> all(type(arg) == str for arg in args)
True

So therefore all the arguments to len() in this case are of a single 
type, namely str, and by Dennis' assertion, "converting a result when all 
inputs are of one [type], NO...", should return the same type as all the 
arguments. Which for the avoidance of all doubt is str.


Similarly for the case len([2, 4, 6]), except this time all the arguments 
(all one of them) are lists, and therefore len() should return a list.

Naturally it's a crazy argument. Which is my point. Operators are merely 
a different syntax for functions of two arguments, and any restriction 
that functions must return the same type as all its arguments is just 
crazy.



> > And rightly rejected by many other programming languages, including 
> > modern Python, not to mention calculators, real mathematics and
> > common sense.
> 
> Lost me again.  I was not aware that calculators, real mathematics 
> and common sense were programming languages.

I didn't say they were. Please parse my sentence again.


-- 
Steven



More information about the Python-list mailing list