python中&;是什么意思_Python

什么类型的Python数据结构由“<>”表示,如下所示.

[, , ]

我在使用用于Python3的Flask-SQLAlchemy库时遇到了这个问题.见下面的代码.

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)

[, , ]

解决方法:

它不是数据结构.这就是Flask-SQLAlchemy如何将模型实例表示为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)

它比默认对象更有用.__ 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>]

你可以在__repr__中放置你想要的任何东西,但通常最好明确地将你的对象表示为一个字符串:

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]

我经常看到< ...>用于非有效Python代码的对象表示的字符串,因为许多其他内置对象’__repr__esentations是重构等效对象的有效Python代码.

标签:python,flask-sqlalchemy

来源: https://codeday.me/bug/20190727/1550370.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值