0

I've been reading through some code and came across something that makes me think I understand super less well than I thought I did. What I saw was:

class Model(object): '''Abstract base model class. ''' ... def get_config(self, verbose=0): '''Return the configuration of the model as a dictionary. To load a model from its configuration, use `keras.models.model_from_config(config, custom_objects={})`. ''' config = super(Model, self).get_config() 

Now, the Model class only inherits from object, so why is there a super? Does the object class have a get_config method ? (Not that I can see). Is this some sort of defensive programming technique, in case a class comes between object and Model? If so, how often & why does that sort of thing happen? If not, is there some other reason for this super?

4
  • 1
    Check out Python’s super() considered super! - basically, super does not mean "superclass". It means "next in the MRO". If so, how often & why does that sort of thing happen? Always when multiple inheritance is in play. Commented Apr 15, 2016 at 17:04
  • @Rogalski - OK, but the way Model has been defined, isn't object next in the mro? If so, why look for a method that doesn't seem to be there? Commented Apr 15, 2016 at 17:38
  • 1
    Yes, it is, but as comment says, it's an abstract base model class.. Concrete implementations would most likely use multiple inheritance to inject correct get_config() method. Disclaimer: I'm not familiar with keras source. Commented Apr 15, 2016 at 17:40
  • @Rogalski - Thanks, you're absolutely right. There is multiple inheritance involved and the super was used to help move through classes in the mro. Commented Apr 18, 2016 at 15:10

1 Answer 1

1

object does not have a valid instance method get_config() so this code, specifically, should not work. However, super essentially takes the parent class of a derived class. This is Python2 syntax. In Python3, you can simply call super() with no parameters.

Anyways, here's an example:

class Number(int): def __eq__(self, other): equal = super(Number, self).__eq__(other) print("{} and {} are {}equal".format(self, other, "not " if not equal else "")) return equal def __str__(self): return "Number({})".format(super(Number, self).__str__()) i = Number(5) j = Number(7) print(i == j) # Output: """ Number(5) and Number(7) are not equal False """ 

This inherits from int and will modify the instance methods of int, while still being able to use the original methods.

In order for this code to work, you could overload the object class (I do not recommend this...).

class object(object): def __init__(self): pass def get_config(self): print("Getting object Config") return "config" class Foo(object): def __init__(self): self.config = None def get_config(self): self.config = super(Foo, self).get_config() print("Getting Foo Config") i = Foo() i.get_config() print(i.config) 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer, but the code does work (it's a snippet of Keras code). The trick is (as @Rogalski explained) that Model is an abstract class, so the super is used to help go to the next method as determined by the mro. This seems (I think) to depend on classes that inherit from the Model class having multiple inheritancee

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.