Python Newbie

Oscar Benjamin oscar.j.benjamin at gmail.com
Sun Feb 24 20:44:39 EST 2013


On 25 February 2013 01:24, Chris Angelico <rosuav at gmail.com> wrote:
> On Mon, Feb 25, 2013 at 11:45 AM, Oscar Benjamin
> <oscar.j.benjamin at gmail.com> wrote:
>> On 25 February 2013 00:08,  <piterrr.dolinski at gmail.com> wrote:
>> Chris Angelico wrote:
>>>> For example (I believe it's already been mentioned) "declaring" intX with some integer value does *nothing* to maintain
>>>>
>>>> X as an integer:
>>>>
>>>> --> intX = 32
>>>>
>>>> --> intX = intX / 3.0
>>>>
>>>> --> intX
>>>>
>>>> 10.6666666666
>>>>
>>>
>>> Yes I did see that it is possible to redefine the type of a variable. But I don't think I would ever do this intentionally; need to be really careful with Python.
>
>> The trickier cases are ones where two types are very similar and can
>> be used similarly in most, but not all, situations. An example of this
>> would be the one that Chris has highlighted where an object that you
>> expected to be an int is actually a float. I find that I need to be
>> careful when using division on quantities that I expected to be
>> integers (true in all languages) and careful about the notation used
>> in a numeric literal. Once you get used to it, you will find it easy
>> to see that the '.0' that Chris appended was deliberate in order to
>> control the type of the resulting object.
>
> Once again, Ethan gets the short end of the citations stick...
> 'twarn't me wrote that, he did. Not that it's at all contrary to my
> views, and I might well have said it if he hadn't, but credit should
> go his direction :)

Apologies to you both. The information was missing and I attempted to
fill it in but got it wrong.

> Note though that in Python 3, you don't need the explicit .0 to force
> it to float (and __future__ can bring that to Python 2 too). 32/3 ->
> 10.66666, int/int->float.

That's true and I'm very glad of this change in Python 3. However
other issues of float/int ambiguity remain for those (like me) who are
particularly concerned with numbers and their accuracy/exactness. So
it is still necessary in Python 3 to be careful around this issue.
While it is an issue in all languages, it is one that is often easier
to deal with in statically typed languages than in Python.


Oscar



More information about the Python-list mailing list