learning python ...

hw hw at adminart.net
Mon May 24 01:20:39 EDT 2021


On 5/23/21 10:02 PM, Stestagg wrote:
> 
> 
> On Sun, 23 May 2021 at 20:37, hw <hw at adminart.net 
> <mailto:hw at adminart.net>> 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. 
> 
> 
> Shadowing is effectively saying “within this bit of code, (scope) I’m 
> going to use an already-used name for my own value”

That should give at least a warning.

>     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. 
> 
> 
> There are some reasons why allowing this is quite nice. And there’s 
> actually a ton of corner cases to consider when thinking about changing 
> the rules

Perl has a way to turn off unwanted warnings.  It won't change the rules 
to give a warning.

> Interestingly python 3 made this a little bit better by stopping you 
> from rebinding (shadowing) a number of built ins, such as True and False.
> 
> In your case, I agree that it is super confusing.

It seems dangerous and seems to show that python is too unfinished to be 
used.  For all I know, it makes it easy to, for example, drop a whole 
table in a database because something was shadowed without warning.

I can imagine that there can be all kinds of situations in which 
something like that happens, and you can spend hours or days trying to 
figure out what's wrong and may never find it.

> One thing to learn to 
> look out for is if you assign to a name, then use that name on a 
> different context, expecting it to be different, then that’s not likely 
> to work as you expect.

Then why doesn't give it at least a warning?

There is even no indication from the output from the program before it 
aborts with an error message that something might be wrong:  For 
'type(float)', it prints "<class 'float'>" just like it does for int. 
How is anyone supposed to debug stuff like that?

Why doesn't print(type(float)) give an error message after the variable 
type was already defeated (or prints something else)?  What is it 
actually printing?

>     It seems like a recipie
>     for creating chaos.
> 
> 
> Luckily almost every python code checker and/or linter will highlight 
> this for you.
> 
> If you’re learning python, I’d highly recommend doing so in an ide or 
> editor that has a code checker running.

Emcas highlights the syntax fine; I don't know if it can do more for 
python.  It shouldn't need to.

Things get creepy when a programming language makes it so that the 
programmer can't figure out anymore how a result produced by his program 
has come about.


More information about the Python-list mailing list