learning python ...
hw
hw at adminart.net
Sun May 23 15:34:29 EDT 2021
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