Macros in python
code-generation, macropy, macros, python
Solution
I don't see a reason why `Object` should need the actual class name as a parameter. You can access the actual class name in `Object` via `self.__class__.__name__`:
class Object(object):
def __init__(self):
self.name = self.__class__.__name__
class SimplePhysicObject(Object):
pass
a = SimplePhysicObject()
print a.name
will print
SimplePhysicObject
This is slightly different than your original code: If you derive from `SimplePhysicObject`, the `name` attribute will be set to the name of the derived class, whereas your original code would continue to use `"SimplePhysicObject"`.
Problem
in my project I have to repeat often such part of code: ``` class SimplePhysicObject(Object): def __init__(self): super(Object, self).__init__('SimplePhysicObject') ``` But instead of `SimplePhysicObject` there is new string each time. Are there any ways to write some macro to make this work easier? Something like: ``` DoTemplate(NewObject) ==> class NewObject(Object): def __init__(self): super(Object, self).__init__('NewObject') ``` UPD Sorry, `Object` is my own class declared before in code