我是一个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()
答案 0 :(得分:1)
Python是缩进敏感的。你的方法需要在类中缩进(即比类声明更多):
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 pick_type(self):
...
答案 1 :(得分:0)