gpt4 book ai didi

Python 3 : What am I doing wrong here?

转载 作者:行者123 更新时间:2023-12-01 04:06:16 28 4
gpt4 key购买 nike

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

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

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

关于Python 3 : What am I doing wrong here?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35536738/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com