On Sep 20, 11:23 am, Peng Yu <pengyu...@gmail.com> wrote:
> On Sun, Sep 20, 2009 at 9:37 AM, Grant Edwards <inva...@invalid.invalid> 
> wrote:
> > On 2009-09-20, Peng Yu <pengyu...@gmail.com> wrote:
>
> >> Suppose I want to define a function that return the minimum number
> >> that can be represented.
>
> >> def f(x):
> >>   #body
>
> >> That it, if I call f(10), f will return the minimum integer that can
> >> be represented in the machine; if I cal f(10.5), f will return the
> >> minimum float that can be represented in the machine.
>
> >> Could somebody let me know what should be in the function body?
>
> > The stuff you wan is in the "sys" module.
>
> > For example:
>
> >>>> sys.float_info
> > sys.floatinfo(max=1.7976931348623157e+308, max_exp=1024,
> > max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021,
> > min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.2204460492503131e-16, 
> > radix=2, rounds=1)
>
> >>>> sys.maxint
> > 2147483647
>
> > You might also want to read up on the type() builtin
>
> I want avoid using any 'if' statement. In C++, I can use template. How
> to do not use 'if' statement in python?
>
> Regards,
> Peng

Python polymorphism is done differently than C++ templates.

Normally you would write your code to be as "type agnostic" as you
reasonable could.  In this case you actually desire to do different
things based on the type of the argument.  Either use an if statement
or you can dispatch on the type using a dict:


mins = {
    float: 2.2250738585072014e-308,
    int: 0,
    long: 0,
}

def f(arg):
    return mins[type(arg)]

(Hint: google for "duck typing".)
HTH,
~Simon
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to