Python Newbie

Chris Angelico rosuav at gmail.com
Sun Feb 24 20:24:38 EST 2013


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 :)

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.

ChrisA



More information about the Python-list mailing list