webdevRefinery Forum: **kwargs with ineritance? - webdevRefinery Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

User is offline lobabob 

  • Group: Members
  • Posts: 98
  • Joined: 21-March 10
  • LocationWouldn't you like to know?
  • Expertise:HTML,CSS

Posted 17 August 2012 - 10:18 AM (#1)

**kwargs with ineritance?


__init__ or super()?
While trawling google for the magic that is python, I came across a question that led to another question.

Question 1 (has to do with inheritance):
class SuperClass:
    def __init__(self, key1 = value, key2 = value, key3 = value):
        self.var1 = key1
        self.var2 = key2
        self.var3 = key3

class SubClass(SuperClass):
    def __init__(self, wthdoiputhere):
        SuperClass.__init__(self, wthdoiputhere)


Is there someway I can use **kwargs in the subclass __init__ so that I can choose which of the key1, key2, key3 variables to pass through?

Edit: I think I found the solution to question 1 here.

Question 2 (came about while searching for an answer to question 1):

Should I even have this line in my subclass
SuperClass.__init__(self, wthdoiputhere)
or should I be using this super keyword I've been hearing about every time I google ineritance?

Edit: Solution to question 2 is here (also applies to question 1).

I'll leave this post here in-case it'll be useful for someone else.
"When the world goes mad, one must accept madness as sanity since sanity is, in the last analysis, nothing but the madness on which the whole world happens to agree." - George Bernard Shaw

Life = http://xkcd.com
0


User is offline ianonavy 

  • Group: Members
  • Posts: 689
  • Joined: 14-April 10
  • Expertise:HTML,CSS,Java,Javascript,Python

Posted 17 August 2012 - 01:35 PM (#2)

Your subclass doesn't even need an
__init__
function if you aren't introducing new keyword arguments and the superclass has all default values for each keyword argument.

Also, don't leave a space between the keyword argument and the default value:
def __init__(self, key1=value, key2=value, key3=value):

reputation += 1 if post.is_helpful else 0
0


Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users


Enter your sign in name and password


Sign in options
  Or sign in with these services