Is there a way to auto generate a __str__() implementation in python?

You can iterate instance attributes using vars, dir, …: def auto_str(cls): def __str__(self): return ‘%s(%s)’ % ( type(self).__name__, ‘, ‘.join(‘%s=%s’ % item for item in vars(self).items()) ) cls.__str__ = __str__ return cls @auto_str class Foo(object): def __init__(self, value_1, value_2): self.attribute_1 = value_1 self.attribute_2 = value_2 Applied: >>> str(Foo(‘bar’, ‘ping’)) ‘Foo(attribute_2=ping, attribute_1=bar)’

“Boilerplate” code in Python?

It is repetitive in the sense that it’s repeated for each script that you might execute from the command line. If you put your main code in a function like this, you can import the module without executing it. This is sometimes useful. It also keeps things organized a bit more. Same as #2 as … Read more

Java error: Implicit super constructor is undefined for default constructor

You get this error because a class which has no constructor has a default constructor, which is argument-less and is equivalent to the following code: public ACSubClass() { super(); } However since your BaseClass declares a constructor (and therefore doesn’t have the default, no-arg constructor that the compiler would otherwise provide) this is illegal – … Read more