learning python ...

hw hw at adminart.net
Mon May 24 09:34:37 EDT 2021


On 5/24/21 9:52 AM, Chris Angelico wrote:
> On Mon, May 24, 2021 at 3:25 PM hw <hw at adminart.net> wrote:
>>
>> 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.
> 
> No, it shouldn't, because it's a deliberate feature. The entire point
> of scoping rules - whether you're in C, Python, REXX, or any other
> language - is to *allow* you to use a name for what you intend it to
> be.
> 
> Does C give you a warning if you create a function-local variable
> called "printf"? No, and it shouldn't. Does any other language
> complain if you use its scoping rules to reuse a name? Nope. Does
> Python? As above, no.

Try the equivalent of above python in C:


void foo(void) {
     int int = 25;
     printf("int: %d\n", int);
}

int main(int argc, char **argv) {
     foo();
}


I don't know which C compiler you're using; gcc doesn't compile this and 
gives several error messages.  Python quietly allows things like this 
without any warning at all, and I'm saying that's a bad thing and that 
python seems unfinished because it doesn't even give a warning in cases 
like this.

I don't know REXX, and I'm not sure what the equivalent would be in 
elisp.  The issue doesn't exist in perl.  It may be intentionally so 
that python makes it easy to defeat fundamental aspects of the language 
simply by, accidentially or intentionally, re-defining them without 
warning, and even if that is so, nobody else has to like a feature like 
that just because you do like it.

Maybe you can show how this is a likeable feature.  I already understood 
that you can somehow re-define functions in python and I can see how 
that can be useful.  You can do things like that in elisp as well.  But 
easily messing up built-in variable types like that is something else. 
Why would I want that in a programming language, and why would I want to 
use one that allows it?

Your claim that I'm insulting python or anoyone is ridiculous. 
According to your logic, C is insulting python.  I suggest you stop 
making assumptions.


The example you want is probably this one:


#include <stdio.h>

void foo(void) {
     int printf = 25;
     printf("int: %d\n", printf);
}

int main(int argc, char **argv) {
     foo();
}


Perhaps you can't see how both examples are different because you're 
looking at things from a python perspective.


More information about the Python-list mailing list