In a dynamic language, why % operator asks user for type info?

mensanator at aol.com mensanator at aol.com
Mon Jul 16 20:23:37 EDT 2007


On Jul 16, 7:10 pm, Karthik Gurusamy <kar1... at gmail.com> wrote:
> Hi,
>
> The string format operator, %, provides a functionality similar to the
> snprintf function in C. In C, the function does not know the type of
> each of the argument and hence relies on the embedded %<char>
> specifier to guide itself while retrieving args.
>
> In python, the language already provides ways to know the type of an
> object.
>
> So in
>
> output = '%d foo %d bar" % (foo_count, bar_count),
> why we need to use %d? I'm thinking some general common placeholder,
> say %x (currently it's hex..) could be used.
>
> output = '%x foo %x bar" % (foo_count, bar_count).
> Since % by definition is string formatting, the operator should be
> able to infer how to convert each of the argument into strings.

You want all your numbers to print in hexadecimal?

>
> If the above is the case, we could've avoided all those exceptions
> that happen when a %d is specified but say a string is passed.

Who does that?

>
> Thanks,
> Karthik





More information about the Python-list mailing list