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?

4
  • Just 'Object'. Commented Nov 3, 2017 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. Commented Nov 3, 2017 at 22:47
  • 1
    Possible duplicate of Python get class name Commented Nov 3, 2017 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. Commented Nov 3, 2017 at 22:49

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.