I am trying to understand a certain OAuth2/web2py integration, but some quirks in the python class model are making it difficult for me to grasp. Specifically, I have this web2py controller:
def google():
if 'state' in request.vars and request.vars.state == 'google':
session.state = request.vars.state
person = Person("google")
print person.render()
return person.render()
So we have are using the Person class here. The implementation is like this:
class Person(Base):
No __init__ is present in the Person class. The Base class has an __init__ function:
class Base(object):
def __init__(
self,
hooks=[],
theme="%(name)s/",
view="app/generic",
meta=None,
context=None
):
from gluon.storage import Storage
self.meta = meta or Storage()
self.context = context or Storage()
self.context.alerts = []
self.context.content_types = []
self.context.categories = []
self.context.menus = []
self.context.internalpages = []
self.theme = theme
self.view = view
# hooks call
self.start()
self.build()
self.pre_render()
# aditional hooks
if not isinstance(hooks, list):
hooks = [hooks]
for hook in hooks:
self.__getattribute__(hook)()
So my questions is as follows: If Person is not explicitly calling Base.__init__, will it be called at all?
Or, to make it more general: will the base class __init__ function be called if the derived class has no __init__ function? And if the derived class has an __init__ function but does not explicitly call the base class __init__ function?
If the derived class has no __init__ function, the parent's class __init__ will be inherited and called.
If the derived class has an __init__ function which does not call the parent's __init__, the parent's __init__ will not be called.
Related
I wrote a Python module, with several classes that inherit from a single class called MasterBlock.
I want to import this module in a script, create several instances of these classes, and then get a list of all the existing instances of all the childrens of this MasterBlock class. I found some solutions with vars()['Blocks.MasterBlock'].__subclasses__() but as the instances I have are child of child of MasterBlock, it doesn't work.
Here is some example code:
Module:
Class MasterBlock:
def main(self):
pass
Class RandomA(MasterBlock):
def __init__(self):
pass
# inherit the main function
Class AnotherRandom(MasterBlock):
def __init__(self):
pass
# inherit the main function
Script:
import module
a=module.RandomA()
b=module.AnotherRandom()
c=module.AnotherRandom()
# here I need to get list_of_instances=[a,b,c]
Th ultimate goal is to be able to do:
for instance in list_of_instances:
instance.main()
If you add a __new__() method as shown below to your base class which keeps track of all instances created in a class variable, you could make the process more-or-less automatic and not have to remember to call something in the __init__() of each subclass.
class MasterBlock(object):
instances = []
def __new__(cls, *args, **kwargs):
instance = super(MasterBlock, cls).__new__(cls, *args, **kwargs)
instance.instances.append(instance)
return instance
def main(self):
print('in main of', self.__class__.__name__) # for testing purposes
class RandomA(MasterBlock):
def __init__(self):
pass
# inherit the main function
class AnotherRandom(RandomA): # works for sub-subclasses, too
def __init__(self):
pass
# inherit the main function
a=RandomA()
b=AnotherRandom()
c=AnotherRandom()
for instance in MasterBlock.instances:
instance.main()
Output:
in main of RandomA
in main of AnotherRandom
in main of AnotherRandom
What about adding a class variable, that contains all the instances of MasterBlock? You can record them with:
Class MasterBlock(object):
all_instances = [] # All instances of MasterBlock
def __init__(self,…):
…
self.all_instances.append(self) # Not added if an exception is raised before
You get all the instances of MasterBlock with MasterBlock.all_instances (or instance.all_instances).
This works if all base classes call the __init__ of the master class (either implicitly through inheritance or explicitly through the usual super() call).
Here's a way of doing that using a class variable:
class MasterBlock(object):
instances = []
def __init__(self):
self.instances.append(self)
def main(self):
print "I am", self
class RandomA(MasterBlock):
def __init__(self):
super(RandomA, self).__init__()
# other init...
class AnotherRandom(MasterBlock):
def __init__(self):
super(AnotherRandom, self).__init__()
# other init...
a = RandomA()
b = AnotherRandom()
c = AnotherRandom()
# here I need to get list_of_instances=[a,b,c]
for instance in MasterBlock.instances:
instance.main()
(you can make it simpler if you don't need __init__ in the subclasses)
output:
I am <__main__.RandomA object at 0x7faa46683610>
I am <__main__.AnotherRandom object at 0x7faa46683650>
I am <__main__.AnotherRandom object at 0x7faa46683690>
In Python, I'm using inheritance for a class. The initial init for the main parent class is below:
def __init__(self, Date = None):
self.Date = Date
self.DatabaseClass = Database()
self.Connection = self.DatabaseClass.databaseConnection()
I've inherited the class into the child class, but am wondering what the correct approach would be to inherit DatabaseClass and Connection variables, i.e., what would be in def __init__?
You just need to call the inherited __init__ method from your own class's __init__ method.
class Child(Parent):
def __init__(self, Date=None, other, arguments):
super().__init__(Date)
# ...
I have a BaseClass and an AbstractClass that inherits from the BaseClass. This is the structure I have in place
class BaseClass(object):
def __init__(self, initialize=True):
self.name = 'base_class'
self.start = 0
if initialize:
self.start = 100
class AbstractClass(BaseClass):
def __init__(self):
self.name = 'asbtract_class'
super(BaseClass, self).__init__()
I want to pass the abstract class an initialize parameter that gets transferred to the base class and if True sets the object's start value to 100.
I tried using the super(BaseClass, self).__init__() but the abstract class gets no start attribute. I get an error when I try to access it.
How can I pass a value the initialize argument to the AbstractClass and use the BaseClass's __init__ method to set the start attribute on the AbstractClass.
The code I used
best = BaseClass()
abs = AbstractClass()
abs.start # AttributeError: 'AbstractClass' object has no attribute 'start'
To invoke the constructor of the super class you should use the class name of the sub class and not the super class, i.e.:
class AbstractClass(BaseClass):
def __init__(self):
super(AbstractClass, self).__init__()
self.name = 'abstract_class'
Note also that I changed the order of invoking the constructor of the super class and setting the name attribute. If you set it before calling the super, the attribute would be overridden by the constructor of the super class, which is most likely not what you intended
And as #Sraw pointed out, for python 3 the notation of calling the super no longer requires the referencing of the class name and can be simplified to
class AbstractClass(BaseClass):
def __init__(self):
super().__init__()
I need to override __import__ function in python when I dynamically import a derived class. (I only have access to the base class code). For example:
Servers=[]
class ServerBase(object):
name='' #name of the specific server class, for each server class
def __init__(self):
self.connected = False
self.name = self.__class__.__module__.capitalize()
Servers.append(self)
When a derived class is imported I need to call __init__ of the base class to add it to Servers[] list, so when in the main module I call:
__import__('DerivedClassName')
Base __init__ will be called
I ended up metaclassing the Servers class:
Servers=[]
''' Servers Metaclass that handles addition of servers to the list '''
class MetaServer(type):
def __init__(self, name, bases, attrs):
self.name = name.capitalize()
Servers.append(self)
super(MetaServer, self).__init__(name, bases, attrs)
class ServerBase:
__metaclass__ = MetaServer
name='' #name of the specific server class, for each server class
def __init__(self):
self.connected = False
That way every time a derived class was imported meta-init got called.
Exactly what I wanted.
Thanks #MartijnPieters
I have the following base class:
class NeuralNetworkBase:
def __init__(self, numberOfInputs, numberOfHiddenNeurons, numberOfOutputs):
self.inputLayer = numpy.zeros(shape = (numberOfInputs))
self.hiddenLayer = numpy.zeros(shape = (numberOfHiddenNeurons))
self.outputLayer = numpy.zeros(shape = (numberOfOutputs))
self.hiddenLayerWeights = numpy.zeros(shape = (numberOfInputs, numberOfHiddenNeurons))
self.outputLayerWeights = numpy.zeros(shape = (numberOfHiddenNeurons, numberOfOutputs))
now, I have a derived class with the following code:
class NeuralNetworkBackPropagation(NeuralNetworkBase):
def __init__(self, numberOfInputs, numberOfHiddenNeurons, numberOfOutputs):
self.outputLayerDeltas = numpy.zeros(shape = (numberOfOutputs))
self.hiddenLayerDeltas = numpy.zeros(shape = (numberOfHiddenNeurons))
But when I instantiate NeuralNetworkBackPropagation I'd like that both constructors get called.This is, I don't want to override the base class' constructor. Does python call by default the base class constructor's when running the derived class' one? Do I have to implicitly do it inside the derived class constructor?
Does python call by default the base
class constructor's when running the
derived class' one? Do I have to
implicitly do it inside the derived
class constructor?
No and yes.
This is consistent with the way Python handles other overridden methods - you have to explicitly call any method from the base class that's been overridden if you want that functionality to be used in the inherited class.
Your constructor should look something like this:
def __init__(self, numberOfInputs, numberOfHiddenNeurons, numberOfOutputs):
NeuralNetworkBase.__init__(self, numberOfInputers, numberOfHiddenNeurons, numberOfOutputs)
self.outputLayerDeltas = numpy.zeros(shape = (numberOfOutputs))
self.hiddenLayerDeltas = numpy.zeros(shape = (numberOfHiddenNeurons))
Alternatively, you could use Python's super function to achieve the same thing, but you need to be careful when using it.
You will have to put this in the __init__() method of NeuralNetworkBackPropagation, that is to call the __init__() method of the parent class (NeuralNetworkBase):
NeuralNetworkBase.__init__(self, numberOfInputs, numberOfHiddenNeurons, numberOfOutputs)
The constructor of the parent class is always called automatically unless you overwrite it in the child class. If you overwrite it in the child class and want to call the parent's class constructor as well, then you'll have to do it as I showed above.