ucanuupnobb icon indicating copy to clipboard operation
ucanuupnobb copied to clipboard

类的处理。。

Open liujuanjuan1984 opened this issue 5 years ago • 0 comments

What gets printed?

class NumFactory:
    def __init__(self, n):
        self.val = n
    def timesTwo(self):
        self.val *= 2
    def plusTwo(self):
        self.val += 2

f = NumFactory(2)
for m in dir(f):
    mthd = getattr(f,m)
    if callable(mthd):
        mthd()

print(f.val)

2 4 6 8 An exception is thrown

liujuanjuan1984 avatar Nov 11 '19 15:11 liujuanjuan1984