如下面的代码所示,为什么我不能__setattr__在属于重载该方法的类的字典上设置值?我预计那b.hello不会存在。
class MyClass():
datastore = {}
def __init__(self):
self.datastore = {}
def __getattr__(self, key):
return self.datastore[key]
def __setattr__(self, key, value):
self.datastore[key] = value
a = MyClass()
b = MyClass()
a.hello = "err"
print a.hello # err
print b.hello # err
Run Code Online (Sandbox Code Playgroud) 我试图了解下面的python代码中发生了什么.我取2的平方根并将其小数除以1.执行此操作5次保持给出相同的值,但第6次和第7次我得到不同的值.
当输入值与前5次计算中的输入值相同时,为什么输出会在第6次变化?
import math as M
frct = M.sqrt(2)
# 1
frct = 1 / (frct - int(frct))
print frct # 2.41421356237
# 2
frct = 1 / (frct - int(frct))
print frct # 2.41421356237
# 3
frct = 1 / (frct - int(frct))
print frct # 2.41421356237
# 4
frct = 1 / (frct - int(frct))
print frct # 2.41421356237
# 5
frct = 1 / (frct - int(frct))
print frct # 2.41421356237
# 6
frct = 1 / …Run Code Online (Sandbox Code Playgroud)