2

I have this code:

class main():
 params = {}
class a(main):
 def __init__(self):
 self.params['test'] = "111aa"
 print self.params
class b(main):
 def __init__(self):
 self.params['be'] = "222bbb"
 print self.params
a()
#{'test': '111aa'}
b()
#{'test': '111aa', 'be': '222bbb'}

I need from b to print only {'be': '222bbb'} Is there any way how to do it ?

Thanks

asked Aug 20, 2013 at 21:51
7
  • 1
    params is a class attribute. You want an instance attribute. Set self.params in main.__init__, and remember to invoke the superclass constructor in the subclass constructors. Commented Aug 20, 2013 at 21:53
  • Do you really want to share params between subclasses of main? Commented Aug 20, 2013 at 21:53
  • 3
    How do you want to share params between the subclasses, but not have each subclass update the same params? This is a very confusing notion of "share" you have.... Commented Aug 20, 2013 at 22:04
  • 1
    As a side note, class main(): isn't illegal, but it will raise red flags to anyone who reads it. If you really want a classic class for some reason, the idiomatic way to write that is class main:. If you don't have any idea what that last sentence meant, you don't want a classic class; write class main(object):. Commented Aug 20, 2013 at 22:10
  • 1
    Calling your class main at all seems pretty bad form, in my book. It's not actually wrong, but other uses of main are sufficiently well-known and popular as to make it pretty confusing, and potentially troublesome if you ever inadvertently use this class in a program that really does have a main function. Commented Aug 20, 2013 at 22:35

1 Answer 1

6

Try this:

class main(object): # use new-style classes!
 def __init__(self):
 self.params = {}
class a(main):
 def __init__(self):
 super(a, self).__init__()
 self.params['test'] = "111aa"
 print self.params
class b(main):
 def __init__(self):
 super(b, self).__init__()
 self.params['be'] = "222bbb"
 print self.params

Notice that in your code params was defined as a class attribute. By defining it in __init__ of the superclass, we're stating that it's an instance attribute. Also notice that the subclasses call __init__ on the superclass.

In this way, we're making sure that each instance has its own params attribute, without sharing it. You can't expect to share an object between instances and have each one with different values for it, it's a contradiction.

answered Aug 20, 2013 at 21:55
Sign up to request clarification or add additional context in comments.

1 Comment

This is a good answer and a good explanation of the issue. My only suggestion would be to use super() to call main's __init__ method, rather than naming the super class explicitly. That's because super supports multiple inheritance, which is difficult or impossible otherwise.

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.