What type of Python data structure is represented by "< >", like this.
[<Board 1>, <Board 2>, <Board 3>]
I came across this while working with the Flask-SQLAlchemy library for Python3. See code below.
class Board(db.Model):
__tablename__ = 'boards'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(256), unique=True, nullable=False)
description = db.Column(db.String(256))
def __init__(id, name, description):
self.id = id
self.name = name
self.description = description
tuple_boards = Board.query.all()
print (tuple_boards)
[<Board 1>, <Board 2>, <Board 3>]
It's not a data structure. This is just how Flask-SQLAlchemy represents an instance of your model as a string with Model.__repr__:
class Model(object):
...
def __repr__(self):
identity = inspect(self).identity
if identity is None:
pk = "(transient {0})".format(id(self))
else:
pk = ', '.join(to_str(value) for value in identity)
return '<{0} {1}>'.format(type(self).__name__, pk)
It's a whole lot more useful than the default object.__repr__:
In [1]: class Thing(object):
... pass
...
In [2]: [Thing(), Thing(), Thing()]
Out[2]:
[<__main__.Thing at 0x10c8826a0>,
<__main__.Thing at 0x10c8820b8>,
<__main__.Thing at 0x10c8822e8>]
You can put whatever you want in __repr__, but it's usually best to unambiguously represent your object as a string:
In [4]: class OtherThing(object):
... def __repr__(self):
... return "I'm an arbitrary string"
...
In [5]: [OtherThing(), OtherThing()]
Out[5]: [I'm an arbitrary string, I'm an arbitrary string]
I've often seen the <...> string used for object representations that aren't valid Python code, as many other built-in objects' __repr__esentations are valid Python code that reconstructs an equivalent object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With