0
What does" "Queue({})".format(self._hiddenlist)" mean ?
class Queue: def __init__(self, contents): self._hiddenlist = list(contents) def push(self, value): self._hiddenlist.insert(0, value) def pop(self): return self._hiddenlist.pop(-1) def __repr__(self): return "Queue({})".format(self._hiddenlist) queue = Queue([1, 2, 3]) print(queue) queue.push(0) print(queue) queue.pop() print(queue) print(queue._hiddenlist)
13 Réponses
+ 2
Intermediate Depression
They wanted the __repr__() to return a nicely formatted string in the form:
"Queue([1, 2, 3])"
so they couldn't use just `self._hiddenlist` as it would return just:
"[1, 2, 3]"
The other option could be an f-string, as your first option, but without extra enclosing curly braces
`f"Queue({self._hiddenlist})`
But they didn't introduce f-strings in this "Python Intermediate" course or "Introduction to Python" course. Only the str.format() function was covered in the latter. So they stuck with what's a learner already knows.
Basically, f-strings and str.format() have the same functionality and can be used interchangeably.
Hope this helps.
+ 3
"Queue({})".format(self._hiddenlist)
is used in the def __repr__() to return a custom string representation when you print(queue).
You can also use f-string
return f"Queue({self._hiddenlist})"
Try commenting out the entire def __repr__ method and run the program. print(queue) will look different if Python default __repr__ is used.
also maybe use double underscore
__hiddenlist
instead of
_hiddenlist
for private data mangling to work.
also: __repr__ or __str__
https://realpython.com/python-repr-vs-str/#:~:text=The%20special%20method%20.,format%20for%20the%20program%27s%20user.
+ 2
The 'format()' function replaces curly braces part ('{}') in the string with the value of the function's argument, i.e. self._hiddenlist. Does it make sense?
+ 2
Intermediate Depression How do you propose doing that? Can you provide an example?
+ 1
Intermediate Depression Can you share a piece of code that you have trouble with? I guess you should have called self.fullname() from __str__(), and you would be OK.
+ 1
Intermediate Depression So what's up with your problematic code? Or do you have no difficulties with it anymore?
+ 1
Intermediate Depression Actually I meant the code with __str__() function you mentioned above.
0
Евгений .....why not just directly write the function's argument? What is my need for format()?
0
Евгений f"{Queue({self._hiddenlist})}" or just self.hiddenlist ? Why use format? (No i have no idea if this is gonna work but i am really struggling to understand str / rpr right now....i was just practising classes earlier and created a method called fullname to return persons fullname but when i tried to print it just showed what i suppose is it's location in the device memory, needless to say it was totally not readable and then got introduced to __str__ which gets called when we use print but it just won't work i am just confused rn.
0
Евгений
Oh not at all you have helped me quite well TYSM ......but if you insist 👀 can you please explain what is property? Setter ? Getter ? For now all i know is that when i use @property i don't have to call the function with the () at the end of it's name + it's no longer editable so basically i can't seem to see any use for it for example in this code i can remove the property decorator and the code won't be effected at all (only used it as it was about the property lesson in this course)
class Player:
def __init__(self, name, lives):
self.name = name
self._lives = lives
def hit(self):
self._lives -= 1
@property
def isAlive(self):
if self._lives > 0:
return True
else:
return False
p = Player("Cyberpunk77", int(input()))
i = 1
while True:
p.hit()
print("Hit # " + str(i))
i += 1
if not p.isAlive:
print("Game Over")
break
0
As for properties, it's a different topic, so maybe you move all your message to a separate question and post a link to it here, and we'll continue there? Or tag me in the new topic.
0
Евгений
Oh well i was unable to print my function because i didn't use () (i didn't know that i had to but figure it out later ) and so i thought that in a class i have to redefine built in function if i wish to use them ( for ex if i wanted to print something i have to define __str__() before hand if i wanted to add then __add__() etc) so i was very confused when i defined __str__() and still didn't print but that turned out wrong and i just had to use () that's all. TY again.