Python 3:我在这里做错了什么?

Python 3: What am I doing wrong here?

本问题已经有最佳答案,请猛点这里访问。

这是我的代码的精简版。当我尝试执行它时,我得到:

Traceback (most recent call last):
File"test.py", line 16, in value = oss.get()
TypeError: get() takes 0 positional arguments but 1 was given

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
1