为什么在带和不带括号的情况下打印时会给出__str __()和__repr __()

完美先生

我正在尝试使用Python在Autodesk Maya(2015)中完成一些装配任务。我遇到了一些我认为很奇怪的事情:

for source, destination in SCENE.Left_BallFoot_orientConstraint.connections(c=1,p=1):
    print source, destination

提供以下输出,__str__()即Attributes的:

Left_BallFoot_orientConstraint.constraintRotateX Left_BallFoot.rotateX
Left_BallFoot_orientConstraint.constraintRotateY Left_BallFoot.rotateY
Left_BallFoot_orientConstraint.constraintRotateZ Left_BallFoot.rotateZ
Left_BallFoot_orientConstraint.constraintRotateOrder Left_BallFoot.rotateOrder
Left_BallFoot_orientConstraint.constraintParentInverseMatrix Left_BallFoot.parentInverseMatrix[0]
Left_BallFoot_orientConstraint.constraintJointOrient Left_BallFoot.jointOrient
Left_BallFoot_orientConstraint.target[0].targetRotate Left_Toe_Control.rotate
Left_BallFoot_orientConstraint.target[0].targetRotateOrder Left_Toe_Control.rotateOrder
Left_BallFoot_orientConstraint.target[0].targetParentMatrix Left_Toe_Control.parentMatrix[0]
Left_BallFoot_orientConstraint.target[0].targetJointOrient Left_Toe_Control.jointOrient
Left_BallFoot_orientConstraint.target[0].targetWeight Left_BallFoot_orientConstraint.Left_ToeControlW0
Left_BallFoot_orientConstraint.Left_ToeControlW0 Left_BallFoot_orientConstraint.target[0].targetWeight

但是在print语句的参数周围加上括号:

for source, destination in SCENE.Left_BallFoot_orientConstraint.connections(c=1,p=1):
    print(source, destination)

结果输出是这个,__repr__()返回值:

(Attribute(u'Left_BallFoot_orientConstraint.constraintRotateX'), Attribute(u'Left_BallFoot.rotateX'))
(Attribute(u'Left_BallFoot_orientConstraint.constraintRotateY'), Attribute(u'Left_BallFoot.rotateY'))
(Attribute(u'Left_BallFoot_orientConstraint.constraintRotateZ'), Attribute(u'Left_BallFoot.rotateZ'))
(Attribute(u'Left_BallFoot_orientConstraint.constraintRotateOrder'), Attribute(u'Left_BallFoot.rotateOrder'))
(Attribute(u'Left_BallFoot_orientConstraint.constraintParentInverseMatrix'), Attribute(u'Left_BallFoot.parentInverseMatrix[0]'))
(Attribute(u'Left_BallFoot_orientConstraint.constraintJointOrient'), Attribute(u'Left_BallFoot.jointOrient'))
(Attribute(u'Left_BallFoot_orientConstraint.target[0].targetRotate'), Attribute(u'Left_Toe_Control.rotate'))
(Attribute(u'Left_BallFoot_orientConstraint.target[0].targetRotateOrder'), Attribute(u'Left_Toe_Control.rotateOrder'))
(Attribute(u'Left_BallFoot_orientConstraint.target[0].targetParentMatrix'), Attribute(u'Left_Toe_Control.parentMatrix[0]'))
(Attribute(u'Left_BallFoot_orientConstraint.target[0].targetJointOrient'), Attribute(u'Left_Toe_Control.jointOrient'))
(Attribute(u'Left_BallFoot_orientConstraint.target[0].targetWeight'), Attribute(u'Left_BallFoot_orientConstraint.Left_ToeControlW0'))
(Attribute(u'Left_BallFoot_orientConstraint.Left_ToeControlW0'), Attribute(u'Left_BallFoot_orientConstraint.target[0].targetWeight'))
致命错误

即使看起来您正在进行函数调用,也没有。在python 2中,print是一个关键字。因此,您的第二个示例确实是:

print (source, destination)

也就是说,您正在打印一个元组因此,您得到的实际上str是的tuple,它显示的repr各部分。

在最新版本的python中,您可以通过使用以下命令在python 2中获取print函数以实现兼容性:

from __future__ import print_function

然后,这将达到您的预期(但您的第一个示例将无效)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Pandas 按位过滤在带括号和不带括号的情况下给出不同的结果

!r在str()和repr()中做什么?

__repr__ 和 python 解包

如何在不带括号和逗号的情况下打印列表python

repr(C)和repr(rust)有什么区别?

__str__和__repr__之间的区别?

repr() 没有前导和尾随引号?

__str__和__repr__的目的是什么?

__str__和__repr__有什么区别?

object.__str__ 和 object.__repr__ 有什么作用?警告:它与 Class.__str__ 和 Class.__repr__ 不同

为什么C编译器在05和07情况下给出这些结果?

python的__repr__和__str__的球拍等效项?

如何在不带引号和括号的情况下将整数值写入文件

__repr __()函数的最佳输出类型和编码实践?

__repr __()和仅调用类实例之间的区别

使用__repr __()了解双引号和单引号之间的区别

为什么“groups”和“groups [myname]”会给出不同的结果?

带括号和不带括号的打印字符串的区别

Python在__repr__()中在漂亮打印和普通打印之间切换

JS:为什么在保持变量和变量名相同的情况下声明不带var的变量时,“严格模式”没有显示参考错误

__repr__和__str__在动态类构建中不起作用

将namedtuple的__str__和__repr__行为带入常规类

带和不带括号的类实例化

如何在c / c ++ / java中在源代码中不带双引号和撇号的情况下打印文本

为什么在给出预购,后购和级别订单遍历的情况下构造二叉树是不可能的?

无论如何在不使用 f 字符串或 str() 的情况下打印 int 和 str 吗?

为什么打印整数的__repr__需要括号?

定义带括号和不带括号的名称在语义上有什么区别?

什么时候我们应该使用带括号和不带括号的sizeof