0

i'm trying to run a simple python script, but somehow i got this error:

AttributeError: 'Script' object has no attribute 'run'

My code looks like this:

class Script(object):
  def __init__(self, data1, data2, data3):
     self.data1 = data1
     self.data2 = data2
     self.data3 = data3

     def getData1(self):
         return self.data1

     def getData2(self):
         return self.data2

     def getData3(self):
         return self.data3

     def run(self):
         return 'Running successfully'

 script1 = Script('data1', 'data2', 'data3')
 print script1.run()

Would be great if someone can help me :)

5
  • Please fix your indentation. It's likely the cause of your problem, but the code in the question just throws an IndentationError. Commented May 23, 2018 at 15:43
  • It looks like all your methods are defined inside the __init__ method rather than in the class. De-denting them by 1 level should fix your problem. Commented May 23, 2018 at 15:46
  • your run() and other methods() are defined under __init__() identation Commented May 23, 2018 at 15:48
  • thanks its working now :) Commented May 23, 2018 at 15:48
  • Voted to close on the basis of typographical error. Commented May 23, 2018 at 15:52

3 Answers 3

1

your run() and other methods() are defined under init() identation Try this:-

class Script(object):
    def __init__(self, data1, data2, data3):
        self.data1 = data1
        self.data2 = data2
        self.data3 = data3

    def getData1(self):
        return self.data1

    def getData2(self):
        return self.data2

    def getData3(self):
        return self.data3

    def run(self):
        return 'Running successfully'

script1 = Script('data1', 'data2', 'data3')
print script1.run()
Sign up to request clarification or add additional context in comments.

Comments

1

From the way the code is formatted in your question, it seems like all functions getData1, getData2, getData3 and run are closures of the method __init__, not methods of the class themselves. This means that they are not methods of the object instance, and hence not accessible from outside __init__.

You can reformat the code to look like this instead

class Script(object):
    def __init__(self, data1, data2, data3):
        self.data1 = data1
        self.data2 = data2
        self.data3 = data3

    def getData1(self):
        return self.data1

    def getData2(self):
        return self.data2

    def getData3(self):
        return self.data3

    def run(self):
        return 'Running successfully'

script1 = Script('data1', 'data2', 'data3')
print script1.run()

which will make all the functions mentioned above methods of the class.

Comments

0

From the first look, you should fix the indentation on the second line.

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.