**之后的参数必须是映射,而不是ChatPermissions

问题描述

当我打印对象时:

print(permissions)

它的输出是这样的:

{'can_send_messages': True,'can_send_media_messages': True,'can_send_polls': True,'can_send_other_messages': True,'can_add_web_page_previews': True,'can_change_info': True,'can_invite_users': True,'can_pin_messages': True}

就像一个映射对象 但是当我尝试将其作为参数时:

some_method(id,**permissions)

它给出了错误

 argument after ** must be a mapping,not ChatPermissions

问题是什么,我该如何解决

解决方法

假设ChatPermissionstelegram库中的类,那么您可以这样做:

some_method(id,**permissions.to_dict())

通常,您还可以使用__dict__来获取对象属性的字典:

some_method(id,**permissions.__dict__)

根据source codeto_dict方法本身使用__dict__,但排除了某些属性(bot和以下划线开头的属性)。

,

这不是最优雅的解决方案,但在这种情况下是可行的解决方案:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="https://www.w3schools.com"
xmlns="https://www.w3schools.com"
elementFormDefault="qualified">

<xs:element name="note">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="to" type="xs:string"/>
      <xs:element name="from" type="xs:string"/>
      <xs:element name="heading" type="xs:string"/>
      <xs:element name="body" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

</xs:schema>

import ast some_method(id,**ast.literal_eval(str(permissions))) 的{​​{1}}方法看起来像是字典,即使它不是字典。因此,我们可以使用它来创建字典,然后创建一个映射对象。