learning python ...
MRAB
python at mrabarnett.plus.com
Sun May 23 15:58:58 EDT 2021
On 2021-05-23 20:34, hw wrote:
> 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.
>
The example you're following calls the variables "myfloat", etc, not
"float", etc, so, yes, you're hiding the names of the types. Don't do that.
As far as Python is concerned, it's just a name that refers to
something. It doesn't care whether that something is a type or a
function or something else, it's just a name that refers to something.
More information about the Python-list
mailing list