The SQLAlchemy ORM tutorial uses this class:
>>> from sqlalchemy import Column, Integer, String
>>> class User(Base):
... __tablename__ = 'users'
...
... id = Column(Integer, primary_key=True)
... name = Column(String)
... fullname = Column(String)
... password = Column(String)
...
... def __init__(self, name, fullname, password):
... self.name = name
... self.fullname = fullname
... self.password = password
...
... def __repr__(self):
... return "<User('%s','%s', '%s')>" % (self.name, self.fullname, self.password)
Why would you go to all the trouble of having a string that will work nicely when eval()‘d, only to break that functionality but surrounding it with angle brackets?
I realise that the eval(repr(foo)) idiom is far from the only purpose of __repr__, but it it still seems odd how it seems to deliberately be disabled here. Is there some greater logic to this that I’m missing, or is it just some arbitrary decision?
Bear in mind that
evalis not used too much; crafting strings for it (or checking if they really already work) is just unnecessary extra effort.Putting the angle brackets in without second thought is much easier, and doesn’t give people ideas about using
eval(which is dangerous if you’re not careful).In other words, there was no deliberate decision to break
eval(repr(x))here. It’s just customary to put angle brackets around__repr__output.