0

i am started to learn Python. I found Dijkstra's Algorithm in german language and try execute the functions in different class as shown below.

Knoten: nodes Kanten: edges Nachbar : neighbour kantenZuNachbarn : edges to neighbour refStartKnoten : start node refZielKnoten: end node

**# Autor: kb **# Datum: 15.07.10

from xml.dom.minidom import

class Knoten(object):

def __init__(self, nameKnoten):
    self.name = nameKnoten
    self.kantenZuNachbarn = []
    self.daten = []

def addNachbar(self, refKante):
    self.kantenZuNachbarn = self.kantenZuNachbarn + [(refKante)]

class Kante(object):

def __init__(self, refStartKnoten, refZielKnoten):
    self.startKnoten = refStartKnoten
    self.zielKnoten = refZielKnoten
    self.daten = []

Her is what i try to do:

#obj of class knoten

k1 = Knoten('A')

k2 = Knoten('B')

#obj in class Kante

kante1 = Kante (k1,k2)

After i created the object, i try to get the attribute of this object by executing following code

kante1.startKnoten

This is what i get from executing the code above. What does that mean? What did i do wrong? <graph_dijkstra.Knoten at 0x1d42a2dd148>

chern
  • 13
  • 1

1 Answers1

0

<graph_dijkstra.Knoten at 0x1d42a2dd148> is how python represents that object in memory. If you print an object of a class that doesn't have a __repr__ or __str__ method, you will get something like that. If you want to see the actual values in it, you either need to add a __repr__ method to the class, or try to access actual values, such as name.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Cz_
  • 371
  • 2
  • 8
  • by executing kante1.startKnoten, i was expecting k1. That is why i thought is an error. Here i try to check the starting node for kante1. – chern Jun 30 '20 at 20:20
  • Ah gotcha. Yeah, if you want it to explicitly say "k1", then you need to have some sort of way of checking an actual value of the node. If you just check the object without a `__repr__` then you'll always get something like that. – Cz_ Jul 01 '20 at 13:08