Skip to content
Advertisement

Tag: class

How can show the singleton object’s attribution?

We can show instance’s attribution with dir function. Now create a class with metaclass method: Show Cls’s _instances attrubution: Why no string _instances in dir(Cls)? Answer Because it’s stored on the metaclass. To be clear, this has nothing to do with the singleton aspect.

Python/Pygame : “pygame.error: display Surface quit”

So, I’m making a game project, and I decided, for once, to make a custom class for my pygame window, like so : And, in the gameloop, I decided to make an instance of this class, and to call the method, like so : But when I try to run the main loop, it gives me this error message (that

Invoking a constructor in a ‘with’ statement

I have the following code: Running it produces the following output: But I expected it to produce: Why isn’t the code within my first example called? Answer The __enter__ method should return the context object. with … as … uses the return value of __enter__ to determine what object to give you. Since your __enter__ returns nothing, it implicitly returns

Inherit choice class to extend it?

I have a field in my models.py that accepts choices determined in a class: The choice class is this: My doubt is how can I inherit this UserChoices class to another choice class, in order to extend it with another options. I tried the following: But it gives me a migration error: Obviously this example is simplified, the actual code

how to pass variables to method

I want to pass variables to the method. which of the following is better? (x and y are constants) or: Answer I’d argue the second one is better with regards to function purity, though this is a principle of Functional Programming rather than Object Oriented Programming. The only thing I’d wonder then is why you’d even make it a member

Advertisement