On Sun, 23 May 2021 at 20:37, hw <h...@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” 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 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. 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. 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. > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list