As far as I know that's not possible, however you can call the init method of the superclass, like this: class inheritedclass(initialclass): def __init__(self): initialclass. __init__(self) self. Attr3 = 'three.
Just call the parent's __init__ using super: class inheritedclass(initialclass): def __new__(self): self. Attr3 = 'three' super(initialclass, self). __init__() I strongly advise to follow Python's naming conventions and start a class with a Capital letter, e.g. InheritedClass and InitialClass.
This helps quickly distinguish classes from methods and variables.
1 Specifically, use super(DerivingClass, self). __init__(). Or just super().
__init__() in Python 3. Edit: -1 because 1. Super needs the inheriting(!) class and 2.
You override __new__ needlessly, and the code as of now is in fact incorrect (it creates a class attribute attr3). – delnan Apr 12 at 14:04 this works with the new method but as lazyr told me (correctly) that I shouldn't use new, I think the correct answer for me yexo's. Thanks though!
– Anake Apr 12 at 14:18 @Anake Credit where credit is due, it was @delnan who told you not to use __new__, I just explained what your code actually did. By the way, __new__ must return an instance of its class if you implement it. In the above answer, and in your code too, inheritedclass() is None!
In other words, it doesn't work at all. (I mistakenly gave this answer an upvote, didn't read the code properly, and now I can't change it :(.) – lazyr Apr 12 at 15:08.
It's incredibly simple. Define a new __init__ method and call the parent's __init__ at the beginning. # assuming a class Base, its __init__ takes one parameter x class Derived(Base): def __init__(self, x, y): # whatever initialization is needed so we can say Derived is-a Base super(Derived, self).
__init__(x) # now, add whatever makes Derived special - do your own initialization self. Y = y In Python 3, you don't have to (and therefore propably shouldn't, for simplicity) explicitly inherit from object or pass the class and self to super.
First of all you're mixing __init__ and __new__, they are different things. __new__ doesn't take instance (self) as argument, it takes class (cls). As for the main part of your question, what you have to do is use super to invoke superclass' __init__.
Your code should look like this: class initialclass(object): def __init__(self): self. Attr1 = 'one' self. Attr2 = 'two' class inheritedclass(initialclass): def __init__(self): self.
Attr3 = 'three' super(inheritedclass, self). __init__().
Thanks,my mistake about the new method. I am trying to use super and I keep getting this error: TypeError: super() argument 1 must be type, not classobj. (I copied and pasted you code exactly as well) – Anake Apr 12 at 14:35 1 @Anake: initialclass needs to inherit object (class initialclass(object)), otherwise it's an old-style class, which is little more than a headache-inducing relic from the past and should be avoided.
– delnan Apr 12 at 14:39 aaah, ok thanks. (also thanks vartec for updating). – Anake Apr 12 at 15:09.
Just call a designated method from the parent's init, if it exists: class initialclass(): def __init__(self): self. Attr1 = 'one' self. Attr2 = 'two' if hasattr(self, 'init_subclass'): self.
Init_subclass() class inheritedclass(initialclass): def init_subclass(self): self. Attr3 = 'three.
1 Virtual -1. No need to do any extra work, let alone hasattr and extra methods, in the parent class. You can simply define a new __init__ that calls the parent's __init__ using super.
– delnan Apr 12 at 14:12 @delnan I know this, but the question was "is there a way of giving the inherited class an init type method which does not override the init method of the parent class". I've provided such a way. This has the added benefit of giving an entrypoint to calling only the subclass init from some other subclass method.
– lazyr Apr 12 at 14:22 1 That's why it's virtual. I know it's what OP asked for, but OP asked "How do I use a hammer to do this? " and the answer is "You use a screwdriver".
– delnan Apr 12 at 14:28 @delnan What I thought OP asked was, to borrow your anology, "How do I use a hammer to do this when I don't want to use a screwdriver? ". I assumed overriding included extending, since to my mind an extended method is simply a type of overridden method -- one that calls the parent method somehow.
I assumed, wrongly it seems, he knew how to extend a method. By the way, aren't all python methods virtual? I've never heard anyone talk about virtual methods in python.
– lazyr Apr 12 at 14:40 You're correct about Python methods being virtual, the -1 is virtual ;) – delnan Apr 12 at 14:43.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.