0

I have a class -

class Start(object):
 def __init__(self):
 self.flag = False
 self.my_list = []
 def set_up(self):
 self.flag = True
 def end_set_up(self):
 self.my_list = [i*2 for i in self.my_list]

And another class which inherits from this class -

class Process(Start):
 def __init__(self):
 super(Process, self).__init__()
 def check_flag(self):
 if self.flag:
 self.my_list = range(1, 10)

And in the third class, I want to do some operations on my_list

class Operation(Start):
 def __init__(self):
 super(Operation, self).__init__() 
 def perform_op(self):
 self.my_list = [i*2 for i in self.my_list]

Now these classes are used in a code snippet as -

start_ob = Start()
start_ob.set_up()
process_ob = Process()
process_ob.check_flag()
op_ob = Operation()
op_ob.perform_op()

My understanding of classes is not that strong. What I thought of achieving with this was -

  • Set up class Start()
  • Inherit flag from class Start() into Process() which should be True now since I called set_up() function here start_ob.set_up()
  • Set my_list in base class to be [1,2....9]
  • Inherit Start() into Operation() and modify list [1,2....9] that I created in the object Process()

But things are not moving according to my understanding. my_list is empty as set_up is False for classes Process and Operation. How do I change my code to make it work according to what my understanding is?

Edit- In the base class, there are two methods, one has to run when the object is initialised, right at the beginning. It will set a flag to True. After which another method in the same base class needs to run according to that flag

asked Aug 6, 2018 at 16:37
10
  • 1
    All your objects are independent with their own instance variables. It seems like you are expecting to some form of prototypal inheritance (a la javascript) but class-based inheritance is different. Commented Aug 6, 2018 at 16:44
  • Thing is in the base class, there are two methods, one has to run when the object is initialised, right at the beginning. It will set a flag to True. After which another method in the same base class needs to run according to that flag Commented Aug 6, 2018 at 16:46
  • Yes, but you have created three independent objects. Commented Aug 6, 2018 at 16:47
  • @juanpa.arrivillaga I think I understand what you are saying. So how do I go about solving this? Commented Aug 6, 2018 at 16:48
  • If you want a flag set to True when you create an object, why don't you set self.flag = True? Commented Aug 6, 2018 at 16:49

1 Answer 1

1

What you’re doing here:

start_ob = Start()
start_ob.set_up()
process_ob = Process()
process_ob.check_flag()
op_ob = Operation()
op_ob.perform_op()

... is creating three entirely separate objects. Each one has its own my_list. Just like you can have three different int objects and they’re all separate values, you can have three different Start objects and they’re all separate values.

What you probably wanted is:

ob = Operation()
ob.set_up()
ob.check_flag()
ob.perform_op()

Now you have a single object, which is an Operation, and therefore a Process, and therefore a Start, so you can call methods from any of those three types and they will affect your object’s value. And now you’re using inheritance.

answered Aug 6, 2018 at 16:42
Sign up to request clarification or add additional context in comments.

3 Comments

Note, Operation isn't inheriting from Process, they are sister classes. It won't have check_flag
Hi, thanks for the quick answer. Can you see whether my edited question affects your answer?
But we can't call check_flag from Operation class as check_flag is a function of Process not Operation?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.