Skip to content

Tag: class

Creating a class property using a function

I found this fantasy name generator here. I am trying to adapt the code to suit my purpose. I want to create an NPC name automatically, using the function name_gen within the class NPC. With the NPC characteristics being: The code from the name generator I need is the following: Now the only thing I think I s…

asdict() inside .format() in a Python class

How can I use asdict() method inside .format() in oder to unpack the class attributes. So that instead of this: I could write something like this: Answer asdict should be called on an instance of a class – self, in this case. Additionally, you don’t need the dict’s keys, you need its values,…

assert menu.__str__() == “” AssertionError

I get assertion error when i try to assert “” (empty string) into str function. Could anyone enlighten me as of why that happens. Totally clueless here. I know the str function needs to return a string but as far as i know it is returning a string when i run: The error comes when i run: here is my