Instance of an object in python

2
from Tree import Node

q = Node(p)

The problem with the code is that I get this message:

  

'Node' is not callable   This inspection highlights attempts to call objects which are not callable, like, for example, tuples.

This does not let me access the methods of the Node class

class Node:
   txt = 0
   izq = None
   der = None

   def __init__(self, txt):
      self.txt = txt
      self.izq = None
      self.der = None

#Setters
   def setTxt(self, ntxt):
      self.txt = ntxt

   def setIzq(self, nIzq):
      self.izq = nIzq

   def setDer(self, nDer):
      self.der = nDer

#Getters
   def getTxt(self):
      return self.txt

   def getIzq(self):
      return self.izq

   def getDer(self):
      return self.der
    
asked by Raul Juliao 22.07.2017 в 01:14
source

2 answers

3

So you comment Tree is actually a package that inside contains a module called Node , which in turn contains a class called Node . Something like this:

Assuming that __init__.py is empty, some of the correct ways to import from main.py are:

from Tree import Node

q = Node.Node(p)
from Tree.Node import Node

q = Node(p)
import Tree.Node

q = Tree.Node.Node(p)

Personally I prefer to name the module with lower case and the class contained in it with a capital letter, it is more readable that is everything.

    
answered by 22.07.2017 в 03:22
1

In the code you shared there are several things to improve.

On the one hand, the variable 'p' is not defined. We could fix it in the following way.

from Tree import Node
p = 1 # un valor cualquiera
q = Node(p)

On the other hand, you have to see where you defined the Node class. You could start by defining it in the same file so you see if it works:

class Node:
   def __init__(self, txt):
       self.txt = txt
       self.izq = None
       self.der = None

p = 1 # un valor cualquiera
q = Node(p)

In Python, you do not need to define the attributes of the class outside of the constructor and you do not need the getters and setters as defined in java. In python all attributes are public, unless they start with a double low script eg: self .__ value = x. What does exist is a decorator called property.

@property
def valores(self):
    return self.__valor

You can look at a bit of this here: link link

    
answered by 22.07.2017 в 23:25