0

So basically what I'm trying to do is

class Object:
    _our_name = type(self).__name__
    __tablename__ = _our_name
    uid = Column(Integer, Sequence(_our_name + '_id_seq'), primary_key=True)
    name = Column(String, unique=True)

but of course I can't use self in this case. How would I do this?

Kahr Kunne
  • 332
  • 1
  • 3
  • 14
  • Just `'Object'`. – user2357112 Nov 03 '17 at 22:44
  • Yes, I realize what it's supposed to be. However this is supposed to be an abstract class, which other classes then inherit from. – Kahr Kunne Nov 03 '17 at 22:47
  • 1
    Possible duplicate of [Python get class name](https://stackoverflow.com/questions/14513019/python-get-class-name) – Aran-Fey Nov 03 '17 at 22:47
  • "However this is supposed to be an abstract class, which other classes then inherit from" - are you expecting all that `_our_name` stuff to be replaced with the subclass name, for each subclass? It sounds like you'd be better served by a class factory of some sort. – user2357112 Nov 03 '17 at 22:49

1 Answers1

1

Unfortunately you can't. During the class' body execution, the class itself is not yet defined.

You can always create the class and change it later, or create the class using type(...)/metaclasses. But you shouldn't go this route unless you really need it.

Governa
  • 908
  • 10
  • 21