6

I have 2 scripts, 1st is All_Methods, and another is All_Testcases, as I am using unittest framework, so here we go.

All_Methods is like:

class All_Services():
    def abc(self):
        x =1

    def bca(self):
        print "My Name is Taimoor"
        self.abc()

    def cba(self):
        self.bca()

and on another script which is All_TestCases is like this:

from All_Methods import All_Services as service

    class All_TestCases(unittest.TestCase):
        def test_1_running_method(self)
            service.cba(self)

Exception showing is:

AttributeError: 'All_TestCases' object has no attribute 'bca'

Kindly someone tell me, what I am missing here? Thanks.

6
  • You seem to have variable indentation width, you should fix that. Commented Oct 15, 2018 at 11:22
  • You should try to do minimal example where you can show use the same problem. You don't need unittest and you don't need any code in the methods to trigger this problem. Commented Oct 15, 2018 at 11:24
  • Actually This is the requirement, which is requird from us. Commented Oct 15, 2018 at 11:39
  • What I mean is that you can reproduce the problem with simpler code to understand and learn how to fix the problem. Commented Oct 15, 2018 at 11:40
  • and its not variable indentation issue, else it would have send me indentationerror. Commented Oct 15, 2018 at 12:19

3 Answers 3

2

You are not using classes in the usual way when you pass in self to methods that you call on the class. Common is to call the methods on instances of the class and getting the self argument implicitly.

When you call Method.running_query_Athena(self) self is an instance of All_TestCases which does not have the method connecting_Athena.

Did you mean for All_TestCases to derive from All_Methods?

Why is All_Methods a class at all?

Sign up to request clarification or add additional context in comments.

1 Comment

Using All_Methods as a class is just a generic example. and I am trying to do what you have mentioned, but all in vein.,
1
  1. Use proper indentation since python is solely based on the basis of how the code is indented.
  2. Please, Please use proper naming conventions; as advised under PEP 8.
  3. You're trying to access an instance method without an instance.

Try the following:

class MyClass:
    def my_instance_method(self):
        return True

    @classmethod
    def my_class_method(cls):
        return True

    @staticmethod
    def my_static_method():
        return True

This won't work:

>> MyClass.my_instance_method()
TypeError: my_instance_method() missing 1 required positional argument: 'self'

but these will since they are not bound to a class instance being created.

MyClass.my_class_method()
MyClass.my_static_method()

An instance method requires that you instantiate the Class; meaning you use:

MyClass().my_instance_method()

Since you seem to want to set response_id on the class instance; using the self argument which denotes the class instance to get the response_id. - I suggest that you use an instance method and instantiate the class as shown above (note the () after the class name)

Kindly do fix your formatting in the question.

3 Comments

While calling the method with creating an object within a script is working, but when I am using the same thing on other script on other class, its not working att all.
Do you mind sharing the entire code? (removing any sensitive data)
My guess is that you're misusing self in the testcases; I think you might be over complicating your life a tiny bit, try using this boto3.amazonaws.com/v1/documentation/api/latest/reference/… - it might help. Also: from All_Methods import All_Methods as Method change this to something more unique. - The question itself is quite difficult to read, if you want update it, and let me know, I'll try to help you. :)
1

There are quite a few things wrong with the code in the example, but putting that aside.

The error is caused by passing an instance of class A as the self argument to a (non-static) method of class B. Python will attempt to call this method on the instance of class A, resulting in the missing attribute error.

Here is a simplified example of the problem:

class A:
    def is_ham(self):
        # Python secretly does `self.is_ham()` here, 
        # because `self` is the current instance of Class A. 
        # Unless you explicitly pass `self` when calling the method.
        return True


class B:
    def is_it_ham(self):
        # Note, `self` is an instance of class B here.
        return A.is_ham(self)


spam = B()
spam.is_it_ham()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.