小编sow*_*owa的帖子

Python,如何在属于重载方法的类的字典对象上使用 __setattr__ ?

如下面的代码所示,为什么我不能__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 setattr python-2.7

5
推荐指数
1
解决办法
8506
查看次数

关于连续分数的Python代码

我试图了解下面的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)

python

0
推荐指数
1
解决办法
4437
查看次数

标签 统计

python ×2

python-2.7 ×1

setattr ×1