Title: Conversion of Number to String(str(number))
Type: crash Stage: resolved
Components: Versions: Python 3.6
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Khalid Moh'd., eric.smith, steven.daprano
Priority: normal Keywords:

Created on 2018-05-18 19:30 by Khalid Moh'd., last changed 2018-05-19 13:45 by steven.daprano. This issue is now closed.

Messages (3)
msg317049 - (view) Author: Khalid Moh'd. (Khalid Moh'd.) Date: 2018-05-18 19:30
Consider conversion of an integer to string:
a=5 #number
str #outputs <class 'str'>
str(a) #works perfectly and prints '5'

Now, consider:
str="Hello World" #reads the string
str #prints "Hello World"
str(5) #gives an error

Interpreter considers variable str before parenthesis not str from <class 'str'>
There must be a way to distinguish user defined variable str and conversion operation str().
msg317057 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-05-18 20:01
The way to avoid this problem is to not assign to str. You should not shadow python builtins that you want to continue using.
msg317118 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2018-05-19 13:45
Also note that shadowing builtins *deliberately* is a powerful and useful technique used for advanced programming.
Date User Action Args
2018-05-19 13:45:28steven.dapranosetnosy: + steven.daprano
messages: + msg317118
2018-05-18 20:01:14eric.smithsetstatus: open -> closed

type: compile error -> crash

nosy: + eric.smith
messages: + msg317057
resolution: not a bug
stage: resolved
2018-05-18 19:30:27Khalid Moh'd.create