learning python ...

Schachner, Joseph Joseph.Schachner at Teledyne.com
Mon May 24 10:42:40 EDT 2021


OMG that is awful abuse of Python!  You have overloaded two Python keywords by making variables of that name.  As a result, float is no longer a type name, it is a variable name that refers to the value 6.67 !

Type(int) is int; type(float) is float, but isinstance(int,float) doesn't work because float is not a type in your script because you assigned float=6.67 and the local variable dictionary is searched first!

To fix this, make your variable name myfloat.   Change it wherever the variable name is wanted.  In particular, the last line should be print(isinstance(myfloat, float)).  The first argument is the variable, the second should be type name.

--- Joseph S.



Teledyne Confidential; Commercially Sensitive Business Data

-----Original Message-----
From: hw <hw at adminart.net> 
Sent: Sunday, May 23, 2021 3:34 PM
To: python-list at python.org
Subject: Re: learning python ...

On 5/23/21 7:28 PM, Peter Otten wrote:
> On 23/05/2021 06:37, hw wrote:
>>
>> Hi,
>>
>> I'm starting to learn python and have made a little example program 
>> following a tutorial[1] I'm attaching.
>>
>> Running it, I'm getting:
>>
>>
>> Traceback (most recent call last):
>>    File "[...]/hworld.py", line 18, in <module>
>>      print(isinstance(int, float))
>> TypeError: isinstance() arg 2 must be a type or tuple of types
>>
>>
>> I would understand to get an error message in line 5 but not in 18.  
>> Is this a bug or a feature?
> 
> It is a bug in your code (which you don't provide). Did you assign 
> some value to float, e. g.:
> 
>  >>> float = 42.0
>  >>> isinstance(int, float)
> Traceback (most recent call last):
>    File "<pyshell#313>", line 1, in <module>
>      isinstance(int, float)
> TypeError: isinstance() arg 2 must be a type or tuple of types
> 
> If you do not shadow the built-in you should get
> 
>  >>> isinstance(int, float)
> False
> 

Apparently the attachment was stripped from my message.  I'll put a smaller version directly into this message instead of an attachment:


#!/usr/bin/python

print("world!")

int = 17
print("world", int)

float = 6.670
print("world", float)

foo = 0
print(type(int))
print(type(float))
print(type(foo))

print(isinstance(foo, str))
print(isinstance(int, float))
print(isinstance(float, float))


I don't know about shadowing.  If I have defeated a whole variable type 
by naming a variable like a variable type, I would think it is a bad 
idea for python to allow this without warning.  It seems like a recipie 
for creating chaos.


More information about the Python-list mailing list