我正在尝试通过调用函数来初始化类属性。请看下文。这段代码按预期工作,同时让我感到困惑。据我了解,getmemtoto 是一个所谓的未绑定函数,需要用类 T 的实例调用。在类定义期间如何调用它?
class T:
def getmemtot():
output=shell(r"sed -r -n -e 's/^MemTotal:\s*([[:digit:]]+)\s*kB/\1/ p' /proc/meminfo")
return int(output)
MEMTOT=T.getmemtot()
如果我尝试直接调用 getmemtot,则会收到以下错误:
>>> T.getmemtot()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unbound method getmemtot() must be called with T instance as first argument (got nothing instead)
>>> getmemtot()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'getmemtot' is not defined