-3

I'm having trouble with python inheritance.

This example only outlines the hierarchy for a far more complicated code I can't post here, so bear with me while I give you all the details needed.

I have defined two classes:

class A(pydot.Node):
    def __init__(self):        
        # do some init
        pydot.Node.__init__(self)

    def very_useful_method(self):
        return self._valuable_property

class B(A):
    def __init__(self):
        # do some initialization
        A.__init__(self)


if "__main__" == __name__
    # some code that uses add_node and get_nodes
    retrieved_b_instance.very_useful_method() #error, **Node** object has no attribute very_useful_method...

I'm pretty convinced I didn't get the inheritance process right, and that I'm doing something wrong with the __init__ calls.

I'm using python 2.7.

Any advice?

Trace:

Traceback (most recent call last):
  File "/home/reuts/PycharmProjects/AI4/Ex4_pydot.py", line 399, in <module>
    driver.interact()
  File "/home/reuts/PycharmProjects/AI4/Ex4_pydot.py", line 362, in interact
    self.parse_command(cmd)
  File "/home/reuts/PycharmProjects/AI4/Ex4_pydot.py", line 354, in parse_command
    options[args[0]]()
  File "/home/reuts/PycharmProjects/AI4/Ex4_pydot.py", line 344, in print_report
    print "node cpt: %s" % node.get_cpt()
AttributeError: 'Node' object has no attribute 'get_cpt'

Node is the base class from pydot's implementation (pydot.Node), on top of that there are two more classes I used while inheriting from pydot.Node.

I've now examined the code in pydot.py and I'm suspecting there is a conversion there. Coming from a Java and static-type background I'm slow in understanding the operations in get_node_list() and their effects on the returned instance's type.

Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88
  • 2
    Post the *full* traceback. – Blender Jan 06 '14 at 21:44
  • 2
    Could we please see the full traceback? That might allow us to help more. also, make sure that you initialize A in the b class first. that's a recommended thing to do in python. – Ben Schwabe Jan 06 '14 at 21:53
  • 4
    Also, you may want to post the actual code here, since you're actual code may vary from the simplified version you have here (due to a typo/overload/etc). I've had that happen before and that can get REALLY messy in SE questions when that happens. As a side note, check this out: http://stackoverflow.com/questions/576169/understanding-python-super-and-init-methods?lq=1 – Ben Schwabe Jan 06 '14 at 21:57
  • Apparently you found a bug. Python inheritance indeed doesn't work and no one noticed this before. You should post a bug report. – 6502 Jan 06 '14 at 22:12
  • 2
    @6502 I don't think I found a bug. I wrote in the post that I probably didn't understand python inheritance and c'tors `super` access. If that's your idea of a good comment or a joke I find your taste bad and your comment useless in a technical forum. – Reut Sharabani Jan 06 '14 at 22:14
  • @ReutSharabani: Yes it was a joke, and it's the title of your question that doesn't make sense and shows bad taste: If you don't understand how something works then tell that, i.e. that you don't understand; not that it's not working. – 6502 Jan 06 '14 at 22:19
  • 1
    Also you're posting code that works (sort of... there is a missing semicolon after the `if`) and clearly this is NOT the code that is giving you an error but something you wrote on the fly and not copied and pasted. That code (adding of course a definition for `SomeClassIDidntWrite`) works. – 6502 Jan 06 '14 at 22:23
  • I think that's obvious from the names I'm using and the lack of actual import/implementation to a `ClassIDidntWrite`, but you're right and I've added an explicit message. That's a useful comment. Thanks. – Reut Sharabani Jan 06 '14 at 22:25
  • @bspymaster, I can't post all of the code here. I think that's common and that's the case with the code I'm working on as well. That said, I am trying to get the needed lines from my code without posting something I'm not interested in posting - while still outlining the problem better. – Reut Sharabani Jan 06 '14 at 22:29
  • 1
    If you don't want to post the full code, please post a small example that produces the same problem. – hankd Jan 06 '14 at 22:31
  • 1
    @ReutSharabani: providing a piece of code that works and asking why doesn't work is not ok. Providing an error message about a different program is not ok. Using as title "Python inheritance doesn't work" is also a very bad idea. If you think the problem is about inheritance then please take the time to recreate a minimal case where the problem shows up and post the **real code** and **real error message**. Otherwise you're just wasting time. – 6502 Jan 06 '14 at 22:34
  • Most of the bugs I met are easily solved once you know how to recreate them, so I *don't* get what you're saying. I appreciate your second comment, other than that if you have nothing to contribute you're really not forced to (I should hope...). Putting myself in your shoes, the error message throwing me off to suspect the constructors rather than the 3rd party code wouldn't make me blow steam off on someone who does try to solve his problem and isn't trying to freeload VB script. And yes, I did read http://stackoverflow.com/questions/how-to-ask, but sometimes you can't meet the whole list. – Reut Sharabani Jan 06 '14 at 22:53

1 Answers1

1

I probably found the error. Apparently pydot.Node had a method that doesn't return what I expected of it (pydot is a graph implementation).

The actual nodes are stored as strings and the get_nodes() methods generates them all over again for each get_nodes call, which causes any extension of the pydot.Node class to be practically meaningless if used later on via get_nodes.

Update: Workaround

The way I found to save data while interacting with the implementation is to use the Node instance's obj_dict property, which is restored via the graph's obj_dict['nodes'] obj_dict elements (saved for all added nodes...)

Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88