I created a class Demo
in which I added a constructor with an empty dictionary in it. With the method addSomething
inside the class I add key value pairs to this dictionary. The key which gets added is of type str
and the value of type int
. In another method useKeys
in the same class I wanna access the key which I added to the dictionary. With the keys()
method I only get the key like dict_keys([<__main__.Demo object at 0x7f7cd00c75b0>])
. How can I make the str
who was added visible?
class Demo:
def __init__(self, someString):
self.something = dict()
def addSomething(self, something):
if something not in self.something:
self.something[something] = 0
self.something[something] += 1
def useKeys(self):
#prints dict_keys([<__main__.Demo object at 0x7f7cd00c75b0>])
print(self.something.keys())
something1 = Demo("ABC")
something2 = Demo("DEF")
something1.addSomething(something2)
print(something1.useKeys())
One suggestion in the comments is to use __str__
. I understand that this method gets called always if an object from this class gets printed. But how can I make the key from the dictionary readable? My current implementation does not make the key readable:
def __str__(self):
return "{self.something}".format(self=self)
With the method addSomething inside the class I add key value pairs to this dictionary. The key which gets added is of type str and the value of type int.
No, it is not. The key which gets added is of type Demo
and the value is of type int
. This is why printing the dictionary keys is printing the __repr__
of a Demo
object.
How can I make the str who was added visible?
You did not add any str
. The strings passed as arguments in your code are never used.
You can either write the __repr__
function (which will override the object
object function of the same name, from which every python3 class inherits), or you can use the argument someString
you're already providing and that it's not being used at all, it's only forcing you to provide a string when creating a new instance of a Demo
object:
Actually using the attribute someString
from the constructor in the addSomething
function.
With this solution, the key is indeed of the type str
.
class Demo:
def __init__(self, someString):
self.something = dict()
self.someString = someString # Actually using the string provided at instance time
def addSomething(self, something):
## This method will use the attribute someString from object something instead
if something.someString not in self.something:
self.something[something.someString] = 0
self.something[something.someString] += 1
def useKeys(self):
## keys are now strings
print(self.something.keys())
something1 = Demo("ABC")
something2 = Demo("DEF")
something1.addSomething(something2)
print(something1.useKeys())
Overriding __repr__
, but you require a string anyway so using someString
from the constructor too.
With this solution, the key is of type Demo
, but when you print that key, it'll display a string.
class Demo:
def __init__(self, someString):
self.something = dict()
self.someString = someString # Actually using the string provided at instance time
def addSomething(self, something):
## This method will use the something object as in the original code
if something not in self.something:
self.something[something] = 0
self.something[something] += 1
def __repr__(self):
## When something1.__repr__ is called, it'll display the someString provided at instance time
return self.someString
def useKeys(self):
## keys are objects, but will appear as strings because of the __repr__ method from that object
print(self.something.keys())
something1 = Demo("ABC")
something2 = Demo("DEF")
something1.addSomething(something2)
print(something1.useKeys())
The __str__
function of an object is used in another circumstance, it's not needed in your requirement.
You can just convert it into a list for easy representation:
print(list(something1.useKeys()))
By the way, it does seem like the class you're implementing already exists as collections.Counter
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.