【发布时间】:2017-10-27 07:07:34
【问题描述】:
可以将类实例变量赋值给方法内的局部变量,如:
class Foo(object):
def __init__(self):
self.bar = 'bar'
def baz(self):
# assign instance variable to local variable with a method
bar = self.bar
# do work with local variable
bar = "qux"
# update value of instance variable
self.bar = bar
return self
通过这样做,可以在Foo.baz() 的范围内引用bar 而不是self.bar。
这样做是错误的还是 Unpythonic?
【问题讨论】:
标签: python python-3.x oop