Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9108855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:54:12+00:00 2026-06-17T02:54:12+00:00

Say I have a Graph class and a Vertex class, defined as below Graph.py

  • 0

Say I have a Graph class and a Vertex class, defined as below

Graph.py

class Graph:

def __init__(self):
    self.adjacencyList = {}

def __str__(self):
    return str(self.adjacencyList)

def addVetex(self,key,value):
    if Vertex(key,value) not in self.adjacencyList:
        self.adjacencyList[Vertex(key,value)] = []

Vertex.py

class Vertex:
def __init__(self,key,value):
    self.key = key
    self.value = value

def __str__(self):
    return "Key: ",str(self.key)," Value: ",str(self,value)

def __hash__(self):
    return self.key

if I do this:

G = Graph()
G.addVetex(1,None)
G.addVetex(2,None)
G.addVetex(1,3)
print G

It print out {<Vertex.Vertex instance at 0x110295b90>: [], <Vertex.Vertex instance at 0x110295bd8>: []} But I am expecting something like {"Key:1 Value:None":[]...}

My question is what I am doing wrong? When a diction got print out, why it does not try to call the str function of its keys/values?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T02:54:13+00:00Added an answer on June 17, 2026 at 2:54 am

    I believe the method you want to implement to get the string you want with your current code is Vertex.__repr__, which is what the python dictionary uses to get string representations of keys.

    Here’s a related stackoverflow answer that sheds some light on the difference between __repr__ and __str__

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So say I have this graph class Graph: def __init__(self): self.nodes =[] self.edges =
Probably a very newb C++ question. Say I have a class, vertex, with several
I have an AdjacencyList class and I want all graph algorithms to be seperated
Let's say I have an object graph of Countries / States / Cities, like
Lets say have this immutable record type: public class Record { public Record(int x,
Say I have a select box eg <div data-bind='visible: someProp'> <select class=selectSubWidgets data-bind='options: subWidgets,optionsText:
I have a class Grid which produces a graph paper like grid on in
Let's say I have this graph always a full graph one start node -
Let's say that I want to create a simple graph representation where Vertex and
I have a issue where, I have a converter class say called aConverter as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.