Python Forum

Full Version: inheritence overrides
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I apologize if this is a repeated question, I've tried everything i can think of to find similar threads without success...

anway..


I have a class that derives from another class that proceses an xml
class xmlProcessable(object):
    
    def __init__(self, xmlElement=None):
        super(xmlProcesable, self).__init__(xmlElement)
        if xmlElement: 
            fromXml(xmlElement)

    def fromXml(self, xmlElement):
        #process xml as xmlProcessable
        pass

class movie(xmlProcessable):

    def __init__(self, xmlElement=None):
        super(movie, self).__init__(xmlElement)
        if xmlElement:
            fromXml(xmlElement)

    def fromXml(self, xmlElement):
        super(movie,self).fromXml(xmlElement) #process xml as xmlProcessable
        #process xml as movie
        pass
I'm from a c oriented background (namely c#) where calling an overrided method in a base class will call the override rather than the base method even when called for in the base class unless explicitly specified. I'm wondering if it holds true in python... is it necessary for me to have the same call in the inherited class as in the base class.

        if xmlElement: 
            fromXml(xmlElement)
or can that call be removed from the inherited class' init? since the called fromXml in the inherited class references the base class' fromXml I don't want to create a infinite loop.




To clarify the question:
assuming we have a movie instance called 'movieitem'
when movieitem in initilized (in __init__()) and moves into its superclass initialization super(movie,self).__init__() will calling fromXml() inside the superclass call default to xmlProcessable.fromXML() or elevate to movieitem.fromXml()
The correct code is
if xmlElement is not None:
    self.fromXml(xmlElement)
Unlike C++, there is no implicit argument in methods.

The actual type of 'self' is used, namely it will call the subclass' method. You don't want this block of code in the subclass' __init__() method.
In Python, you don't have to define __init__ function in any class, and if it is a derived class and you want to execute the same code as in the parent class - you don need it in the derived.

Moreover, object - starting rom 2.7 - is a default parent of any class, so inheriting from it explicitly is redundant, and calling to it __init__ is wasteful.

The purpose of __init__ is to initialize object attributes, so if you don't have any - there's no need for it. object class don't have initial attributes - no need to call its __init__

In theory, you can even add attributes "on the fly" (dynamic language) but it is considered a bad practice.

PS In C++, you have to define a constructor. __init__ is not a constructor - Python data model does not even use a word constructor (I checked).
(Aug-24-2018, 09:14 PM)Gribouillis Wrote: [ -> ]The correct code is
if xmlElement is not None:
    self.fromXml(xmlElement)
Unlike C++, there is no implicit argument in methods.

According to python.org's documentation my code was valid unless this has changed between 2.4 and 2.7.15, which I'm currently using. As I understand it anything that doesn't meet the 'falsy' conditions is considered true.

Quote:The actual type of 'self' is used, namely it will call the subclass' method. You don't want this block of code in the subclass' __init__() method.

so, just so I'm clear, you're saying it's sufficient to have the call to fromXml() in the __init__() of superclass xmlProcessable and unecessary in the __init__() of subclass movie

(Aug-24-2018, 09:30 PM)volcano63 Wrote: [ -> ]The purpose of __init__ is to initialize object attributes, so if you don't have any - there's no need for it. object class don't have initial attributes - no need to call its __init__

even though i did not show it there is initialztion necessary (and performed in the init of these two classes... however xmlElement I see it as wasteful to initialize and THEN assign values in seperate functions when if provided xmlElement in the init can initilize and assign value in a single stroke
movieitem = movie(xmlElement)
instead of two lines to do the same
movieitem = movie()
movieitem.fromXml(xmlElement)
I understand that python doesn't use words like constructor, but if a template for initializing values in a instance of a class isn't the equivalent of C/C++/C# constructors i don't know what is. and while it may be bad practice for me to use it as such i find it efficient and useful to do so. Thanks for the suggestion however.

(Aug-24-2018, 09:30 PM)volcano63 Wrote: [ -> ]The purpose of __init__ is to initialize object attributes, so if you don't have any - there's no need for it. object class don't have initial attributes - no need to call its __init__

I'm aware of this, the xmlProcessable is actually a class subclassed from another class in my code, I just changed it to object to minimize confusion (apparently i failed and you focused on that)

I am aware that people are trying to help in correcting my code, and I'm grateful for that, but alot of the code you're trying to correct is set that way for a reason within my extended code (which is too much and obviously would be too distracting from the way my code was attacked from every conceivable angle more than the question was answered. I hope I don't sound ungrateful, because I do appreciate the help.
(Aug-25-2018, 01:55 AM)NobahdiAtoll Wrote: [ -> ]so, just so I'm clear, you're saying it's sufficient to have the call to fromXml() in the __init__() of superclass xmlProcessable and unecessary in the __init__() of subclass movie
Yes. I'm also saying that if you write fromXml instead of self.fromXml, the code won't work.
NobahdiAtoll Wrote:According to python.org's documentation my code was valid unless this has changed between 2.4 and 2.7.15
Your code is valid, however, the best way to test if something is not None is if xmlElement is not None, otherwise if xmlElement is equivalent to if bool(xmlElement). It probably won't change anything in practice here, but if someone passes 0 or [] instead of nothing, it will be detected.
(Aug-25-2018, 05:29 AM)Gribouillis Wrote: [ -> ]Yes. I'm also saying that if you write fromXml instead of self.fromXml, the code won't work.

Nice catch, saved me from having to hunt that down later

(Aug-25-2018, 05:29 AM)Gribouillis Wrote: [ -> ]Your code is valid, however, the best way to test if something is not None is if xmlElement is not None, otherwise if xmlElement is equivalent to if bool(xmlElement). It probably won't change anything in practice here, but if someone passes 0 or [] instead of nothing, it will be detected.

well this part of my code is internally controlled xmlElement will always equate to an xml.etree.ElementTree.Element or None since the user input will never have control over the creation of a movie(xmlElement) or xmlProcessable(xmlElement) or the xmlElement parsed to create it, and i like to use as concise code as possible, there are other places where I do use is not None but only when i need to test for other values individually as well.