【发布时间】:2016-06-29 11:53:59
【问题描述】:
我是一名 Python 初学者,我想将我的代码封装在一个类中。但是,当我运行代码时,我得到一个 AttributeError:'Generating_objects' 对象没有属性 'pick_type'。请告诉我哪里错了?
import random
from random import choice
class Generating_objects(object):
"""This class would be used to generate objects of different types e.g
integers, string, lists and so on"""
def __init__(self):
pass
def generateRandomInt(self):
self.num = random.randint(-100000, 1000000000)
return self.num
def pick_type(self):
lists = ["Int","String","Float","bool"]
choices = choice(lists)
if choices == "Int":
print generateRandomInt()
else:
print "BOO"
genR = Generating_objects()
genR.pick_type()
【问题讨论】:
-
上面的缩进是否与您的代码中的内容匹配?
-
然后点击
edit按钮,再次复制粘贴你的代码,选择代码,然后点击工具栏上的{ }按钮。 ;) -
您问题中的代码与您正在运行的代码不匹配。您问题中的代码会给出 IndentationError.
-
您是否尝试省略了初始化声明?我猜,
pass甚至中断了父object类的初始化,所以你的类根本没有属性 -
我试过你的代码 - 它不会抛出错误,只是打印 BOO
标签: python python-2.7