老师,麻烦帮我看一下,我的代码运行不出来,报错name 'self' is not defined, 谢谢!

老师,麻烦帮我看一下,我的代码运行不出来,报错name 'self' is not defined, 谢谢!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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')


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
时间, 2022-05-08 11:14:53

同学,你好!

1、运行同学提供的代码的报的是其他的错误,可以将私有方法改为普通的实例方法再调用

https://img1.sycdn.imooc.com//climg/6277354b098d6bfd08450078.jpg

https://img1.sycdn.imooc.com//climg/6277356b09c031e907520146.jpg

2、同学可以将报错信息截图多提供一些,可能是其他文件中的错误 

祝学习愉快!

  • 提问者 winnerbob #1

    https://img1.sycdn.imooc.com//climg/6279926e095e3bc312430225.jpg一直在报这样的错误,麻烦老师看看是什么问题,为什么一直在说self没有定义,谢谢

    2022-05-10 06:16:00
  • 时间, 回复 提问者 winnerbob #2

    同学,你好!是utils.py文件中的错误,同学将文件中的代码提供下

    https://img1.sycdn.imooc.com//climg/6279c2a009f8ec1807600076.jpg

    祝学习愉快!

    2022-05-10 09:40:54
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码