0

When I do not crate object for CP class, the operations are not captured. I am referring to the code below, Can somebody help me understand why we need obj creation in this case

from abc import ABC, abstractmethod
class P(ABC):
    def __init__(self):
        super().__init__()
        self._pre_map = {}
        self._pre_order = []

    def set_pre(self, tag_value):
        index = len(self._pre_map)
        print(index)
        self._pre_map[index] = tag_value
        self._pre_order.append(index)

    def execute(self):
        pass

class CP(P):
    def __init__(self):
        super().__init__()

    def execute(self):
        self.prnt()

    def prnt(self):
        print (self._pre_map)
        print (self._pre_order)

#Working
print("\n++++++++ working")
obj = CP()
obj.set_pre("test string added")
obj.execute()

#Not Working
print("\n+++++++ not working")
CP().set_pre("test string added")
CP().execute()

It produces,

++++++++working
0
{0: 'test string added'}
[0]

+++++++not working
0
{}
[]
0

2 Answers 2

1

When you call the class the second time with CP.execute(), you have created a completely new instance of the CP class. It is not going to have the text string you specified.

If you actually wanted it to print the values like the working one you can make the functions return self after each call in the P class. If you did that you could do something like this.

from abc import ABC, abstractmethod
class P(ABC):
    def __init__(self):
        super().__init__()
        self._pre_map = {}
        self._pre_order = []

    def set_pre(self, tag_value):
        index = len(self._pre_map)
        print(index)
        self._pre_map[index] = tag_value
        self._pre_order.append(index)
        ##need to return self here
        return self

    def execute(self):
        pass

class CP(P):
    def __init__(self):
        super().__init__()

    def execute(self):
        self.prnt()


    def prnt(self):
        print (self._pre_map)
        print (self._pre_order)

#Working
print("\n++++++++ working")
obj = CP()
obj.set_pre("test string added")
obj.execute()

#Not Working
print("\n+++++++ not working: but now working after returning self in the P class")
CP().set_pre("test string added").execute()

++++++++ working
0
{0: 'test string added'}
[0]

+++++++ not working: but now working after returning self in the P class
0
{0: 'test string added'}
[0]

This would print the result you want.

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

2 Comments

AttributeError: 'NoneType' object has no attribute 'execute'...
I added additional code. You actually have to change your P class to return self after the set_pre function for what i said to work.
1

The reason for the difference is the fact that in the first one, you are creating an instance, and using that instance the whole way through, whereas in the second one, you are using two different instances of your class.

The two different instances cannot share their attributes, so you are unable to recall what happened. If you really don't want to use a dedicated variable, change your P class to look like this:

class P(ABC):
...
    def set_pre(self, tag_value):
        index = len(self._pre_map)
        print(index)
        self._pre_map[index] = tag_value
        self._pre_order.append(index)
        return self
...

And use CP().set_pre("test string added").execute()

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.