0

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()
6
  • 2
    Does the indentation above match what is in your code? Commented Mar 14, 2016 at 12:32
  • 2
    Then hit the edit button, copy paste your code there again, select the code, then click the { } button on the toolbar. ;) Commented Mar 14, 2016 at 12:35
  • The code in your question cannot match the code you're running. The code from your question would give an IndentationError. Commented Mar 14, 2016 at 12:37
  • Did you try to leave out the init declaration? I guess, the pass breaks initialising even the parent object class, so your class has no attributes at all Commented Mar 14, 2016 at 12:40
  • I tried your code - it doesn't throw an error, just prints BOO Commented Mar 14, 2016 at 12:43

2 Answers 2

1

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):
    ...
Sign up to request clarification or add additional context in comments.

1 Comment

your code also has an indentation error - """docstrings""" must also be indented on the same level as the methods.
0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.