Thanks a lot Nigel.

I am not a big fan of inheritance myself, but in this case composition
wouldn't have worked for me because it would then dilute the fact that User
object is really a node with the label "User" and a bunch of properties.

I like the option you have suggested and will give it a try.

Cheers.



-- Thanks and Regards
   Mahesh Lal


On 5 February 2015 at 16:04, Nigel Small <[email protected]> wrote:

> Hi Mahesh
>
> I'm not a big fan of using *super*. I'm happy to admit that's probably a
> failing on my part but I find the syntax confusing and certainly not in
> line with "readability counts"! On top of that, I've not been able to get
> it working for *Node* inheritance anyway...
>
> So, you can use this notation instead:
>
> from py2neo import Node
>>
>> class User(Node):
>>
>>     def __init__(self, user_id, username, name=""):
>>         assert isinstance(user_id, int) and isinstance(username, str)
>>         Node.__init__(self, "User", user_id=user_id,
>> username=username,name=name)
>>
>>     def name(self):
>>         return self["name"]
>>
>>     def user_id(self):
>>         return self["user_id"]
>>
>>     def username(self):
>>         return self["username"]
>>
>
> This will call the *Node* constructor directly and should work!
>
> Cheers
> Nigel
>
>
> On 5 February 2015 at 02:09, Mahesh Lal <[email protected]> wrote:
>
>> Hi,
>>
>> I started using Py2neo recently. I like the simplicity of the Node class,
>> but considering I wanted to wrap more domain object specific functionality
>> around the Node object, I decided to inherit it.
>>
>> So here is what my class looks like:
>>
>> from py2neo import Node
>>
>> class User(Node):
>>
>>     def __init__(self, user_id, username, name = ""):
>>         assert isinstance(user_id, int) and isinstance(username, str)
>>
>>    super().__init__("User", user_id = user_id, username= username,
>>  name= name)
>>
>>
>>     def name(self):
>>         return self["name"]
>>
>>     def user_id(self):
>>         return self["user_id"]
>>
>>     def username(self):
>>         return self["username"]
>>
>> I create the node using graph.create
>>
>> graph.create(User(user_id=12734, username="maheshkl", name="Mahesh Lal"))
>>
>> I get the following output:
>>
>> (<User graph='http://localhost:7474/db/data/' ref='node/1' *labels={'User',
>> <class 'super'>}* properties={'user_id': 12734, 'username': 'maheshkl',
>> 'name': 'Mahesh Lal'}>,)
>> The weird bit is the *labels. *How is it getting the *<class 'super'> *
>> label?
>>
>> Apologies if this seems like a stupid question that any python programmer
>> should know, but I am confused by this behaviour.
>>
>> I doesnt seem to happen when I compose the Node object within the User
>> object.
>>
>>
>> -- Thanks and Regards
>>    Mahesh Lal
>>
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to