This question already has answers here:
TypeError: worker() takes 0 positional arguments but 1 was given
                                
                                    (6个答案)
                                
                        
                                4年前关闭。
            
                    
这是我的代码的精简版。
当我尝试执行它时,我得到:


  追溯(最近一次通话):
   文件“ test.py”,第16行,值= oss.get()
  TypeError:get()接受0个位置参数,但给出了1个


import os

class OsyncStateSerial():
        """Reads and writes current state serial for local replica"""

        def __init__(self, oss_file):
                if os.path.exists(oss_file):
                        pass
        def ranget():
                return 1

        def ranset():
                return 0

oss = OsyncStateSerial("somefile")
value = oss.ranget()
print(value)


我究竟做错了什么 ?

最佳答案

您需要在类方法中包含参数self

import os

class OsyncStateSerial():
        """Reads and writes current state serial for local replica"""

        def __init__(self, oss_file):
                if os.path.exists(oss_file):
                        pass
        def ranget(self):
                return 1

        def ranset(self):
                return 0

oss = OsyncStateSerial("somefile")
value = oss.ranget()
print(value)


输出量

1

08-24 21:08