老师,麻烦帮我看一下,我的代码运行不出来,报错name 'self' is not defined, 谢谢!
# coding:utf-8
import os
import time
import json
from common.utils import check_file
from common.error import UserExistsError
class Base(object):
def __init__(self, user_json, gift_json):
self.user_json = user_json
self.gift_json = gift_json
self.__check_user_json()
self.__check_gift_json()
def __check_user_json(self):
check_file(self.user_json)
def __check_gift_json(self):
check_file(self.gift_json)
def __read_users(self, time_to_str=False):
with open(self.user_json, 'r') as f:
data = json.loads(f.read())
return data
def __write_user(self, **user): # password active
if 'username' not in user:
raise ValueError('missing username')
if 'role' not in user:
raise ValueError('missing role')
user['active'] = True
user['create_time'] = time.time()
user['update_time'] = time.time()
user['gifts'] = []
users = self.__read_users()
if user['username'] in users:
raise UserExistsError('username %s had exists' % user['username'])
users.update(
{user['username']: user}
)
json_user = json.dumps(users)
with open(self.user_json, 'w') as f:
f.write(json_user)
if __name__ == '__main__':
gift_path = os.path.join(os.getcwd(), 'storage', 'gift.json')
user_path = os.path.join(os.getcwd(), 'storage', 'user.json')
print(gift_path)
print(user_path)
base = Base(user_json=user_path, gift_json=gift_path)
base.write_user(username='Bob', role='admin')10
收起
正在回答 回答被采纳积分+1
1回答
Python全能工程师
- 参与学习 人
- 提交作业 16416 份
- 解答问题 4469 个
全新版本覆盖5大热门就业方向:Web全栈、爬虫、数据分析、软件测试、人工智能,零基础进击Python全能型工程师,从大厂挑人到我挑大厂,诱人薪资在前方!
了解课程


一直在报这样的错误,麻烦老师看看是什么问题,为什么一直在说self没有定义,谢谢
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星