请选择 进入手机版 | 继续访问电脑版
设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
查看: 16031|回复: 2

不知道创建实例变量的正确姿势

[复制链接]

1

主题

0

好友

20

积分

新手上路

Rank: 1

发表于 2019-1-22 17:33:29 |显示全部楼层
  1. class entry:
  2.     """号码薄最基础的条目,只包含一个名字"""

  3.     def __init__(self, name):
  4.         self.name = name

  5.     def __str__(self):
  6.         content = ''
  7.         for i in self.item_content:
  8.             content += '{}:{} '.format(i, self.item_content[i])
  9.         return content

  10.     def set_number(self, number, type='cellphone'):
  11.         self.item_content[type] = number
复制代码
在实例中调用 set_number方法会报错
Traceback (most recent call last):
  File "D:/pythonworkspace/numberbook/numberbook.py", line 71, in <module>
    entries_insert()
  File "D:/pythonworkspace/numberbook/numberbook.py", line 61, in entries_insert
    new_entry.set_number(str(num_input))
  File "D:\pythonworkspace\numberbook\entry.py", line 17, in set_number
    self.item_content[type] = number
AttributeError: 'entry' object has no attribute 'item_content'

不知道该怎么改,求救
回复

使用道具 举报

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

发表于 2019-1-22 23:12:17 |显示全部楼层
哪里有定义过 item_content 这个成员变量?
你代码里只有使用它,没有创建它的地方
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

1

主题

0

好友

20

积分

新手上路

Rank: 1

发表于 2019-1-27 22:42:07 |显示全部楼层
crossin先生 发表于 2019-1-22 23:12
哪里有定义过 item_content 这个成员变量?
你代码里只有使用它,没有创建它的地方 ...

懂了,谢谢老师
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-3-28 16:34 , Processed in 0.015843 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部