Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 将Python变量重新指定给不同的类型有什么问题吗?_Python 3.x_Variable Assignment_Standards - Fatal编程技术网

Python 3.x 将Python变量重新指定给不同的类型有什么问题吗?

Python 3.x 将Python变量重新指定给不同的类型有什么问题吗?,python-3.x,variable-assignment,standards,Python 3.x,Variable Assignment,Standards,下面是我要问的一个例子 我有密码: html = requests.get("https://www.bankofcanada.ca/") soup = BeautifulSoup(html.content, "html.parser") cad = soup.get_text() cad = cad[cad.index("titlesHomepageChart"):cad.index("subTitlesHomepageChart")] cad = cad.replace("titlesHom

下面是我要问的一个例子

我有密码:

html = requests.get("https://www.bankofcanada.ca/")
soup = BeautifulSoup(html.content, "html.parser")
cad = soup.get_text()
cad = cad[cad.index("titlesHomepageChart"):cad.index("subTitlesHomepageChart")]
cad = cad.replace("titlesHomepageChart = ", "").replace(";", "")

cad = json.loads(cad)
print("Currency:\n" + cad['USD'] + "\n")

它按预期工作-用后续值覆盖“cad”的旧值。我想知道这种重新分配方法是否普遍使用。

它本身没有问题,只是如果不是像您在这里所做的那样在一个非常局部的区域内进行,可能会造成混乱。我认为这不是一种好的风格。很难判断哪些变量使用相同的名称。从功能上讲,重用变量来存储不同的类型值是可行的,因为python不需要声明变量类型,也不强制执行严格的类型检查。Python知道存储在变量中的类型。但是重用变量来存储不同的值类型会使代码更难阅读。这是一个典型的问题,您可能会在softwareengineering.stackexchange.com上找到良好的反馈