qwerty ayyy
qwerty ayyy

Reputation: 385

Wrapping code in a Class Python

I am a python beginner and I want to wrap my code in a class. However when I run the code I am getting an AttributeError: 'Generating_objects' object has no attribute 'pick_type'. Please tell me where is wrong ?

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()

Upvotes: 0

Views: 364

Answers (2)

Tim Spence
Tim Spence

Reputation: 67

Python is indentation-sensitive. Your methods need to be indented inside the class (ie further than the class declaration):

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):
    ...

Upvotes: 1

Mohit Rustagi
Mohit Rustagi

Reputation: 316

Indentation in your code is not correct. Check this link for reference. Python: I'm getting an 'indented block' error on the last 3 quotes (""") of my comments under functions. What's up?

Upvotes: 0

Related Questions