question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inheritance and Custom constructors

See original GitHub issue

Hi all,

the documentation is a bit vague about how to do define your own constructor when inheritance is being performed. The documentation is here: https://neomodel.readthedocs.io/en/latest/extending.html

The example given is:

class Item(StructuredNode):
    name = StringProperty(unique_index=True)
    uid = StringProperty(unique_index=True)

    def __init__(self, product, *args, **kwargs):
        self.product = product
        kwargs["uid"] = 'g.' + str(self.product.pk)
        kwargs["name"] = self.product.product_name

        super(Item, self).__init__(self, *args, **kwargs)

that just doesnt work - you can run this yourself and realise that you will get an exception on StructuredNode constructor.

One can make it work with the following:

class Item(StructuredNode):
    name = StringProperty(unique_index=True)
    uid = StringProperty(unique_index=True)

    def __init__(self, product, *args, **kwargs):
        self.product = product
        kwargs["uid"] = 'g.' + str(self.product.pk)
        kwargs["name"] = self.product.product_name

        super(Item, self).__init__(*args, **kwargs)

Still, there’s a problem when the .save() method is being called on that object. This happens because it tries to inflate an object using this constructor, without “product” argument being provided.

a solution would be:

class Item(StructuredNode):
    name = StringProperty(unique_index=True)
    uid = StringProperty(unique_index=True)

    def __init__(self, product = None, *args, **kwargs):
        if product is not None:
            self.product = product
            kwargs["uid"] = 'g.' + str(self.product.pk)
            kwargs["name"] = self.product.product_name

        super(Item, self).__init__(*args, **kwargs)

Which is quite ugly and I believe that probably not the intended implementation. How is the correct way to perform it? I can improve the docs on that if required.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
aanastasioucommented, Feb 20, 2019

@MarcoLotz I think that I may have misinterpreted your question. If you are not looking for advice on a specific problem you are dealing with and you are simply highlighting an issue with the documentation then you are welcome to provide a PR about it.

0reactions
aanastasioucommented, Apr 26, 2019
Read more comments on GitHub >

github_iconTop Results From Across the Web

Inheritance and Constructors in Java - GeeksforGeeks
Constructors in Java are used to initialize the values of the attributes of the object serving the goal to bring Java closer to...
Read more >
Inheriting constructors - c++ - Stack Overflow
Constructors are not inherited. They are called implicitly or explicitly by the child constructor. The compiler creates a default ...
Read more >
17.4 — Constructors and initialization of derived classes
In the past two lessons, we've explored some basics around inheritance in C++ and the order that derived classes are initialized.
Read more >
9.2. Inheritance and Constructors — AP CSAwesome
Subclasses inherit public methods from the superclass that they extend, but they cannot access the private instance variables of the superclass directly and ......
Read more >
Java OOP: The Role of Constructors in Inheritance - Skillsoft
Using our learning experience platform, Percipio, your learners can engage in custom learning paths that can feature curated content from all sources. Learn ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found