liujuanjuan1984 / ucanuupnobb

you can you up, no bb. 自学 python 编程过程中的挑战、笔记及我的践友们。
17 stars 9 forks source link

类的处理。。 #80

Open liujuanjuan1984 opened 4 years ago

liujuanjuan1984 commented 4 years ago

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