Iterate over object attributes in python

I have a python object with several attributes and methods. I want to iterate over object attributes. class my_python_obj(object): attr1='a' attr2='b' attr3='c' def method1(self, etc, etc): #Statements I want to generate a dictionary containing all of the objects attributes and their current values, but I want to do it in a dynamic way (so if … Read more

Using the class as a type hint for arguments in its methods

The code I have included below throws the following error: NameError: name 'Vector2' is not defined at this line: def Translate (self, pos: Vector2): Why does Python not recognize my Vector2 class in the Translate method? class Vector2: def __init__(self, x: float, y: float): self.x = x self.y = y def Translate(self, pos: Vector2): self.x … Read more

Chain-calling parent initialisers in python

Consider this – a base class A, class B inheriting from A, class C inheriting from B. What is a generic way to call a parent class initialiser in an initialiser? If this still sounds too vague, here’s some code. class A(object): def __init__(self): print "Initialiser A was called" class B(A): def __init__(self): super(B,self).__init__() print … Read more