简体   繁体   English

Python dict 解包运算符未在构造函数中解包字符串

[英]Python dict unpacking operator not unpacking string in constructor

I am creating a small app using socketio in python on server side and JS on client side.我正在服务器端的 python 和客户端的 JS 中使用 socketio 创建一个小应用程序。 For one of my events, the client emits a character object that was created by the user in the browser to be persisted by the server.对于我的一个事件,客户端发出一个字符 object,该字符由用户在浏览器中创建,由服务器持久保存。

On the server side, some validations are necessary before persistence, and so i've created the Character class in order to keep all character validations and such, in one area.在服务器端,在持久化之前需要进行一些验证,因此我创建了字符 class 以便将所有字符验证等保留在一个区域中。

Character has a constructor like so: Character 有一个像这样的构造函数:

class Character():
  def __init__(self, stats=None, strength=0, stamina=0, armor=0, agility=0, charisma=0, health=0, lowAttack=0, highAttack=0, attack=0, ability_points=16, characterName=""):
    if stats is None:
        stats = ['strength', 'stamina', 'armor', 'agility', 'charisma']
    self.stats = stats
    self.strength = strength
    self.stamina = stamina
    self.armor = armor
    self.agility = agility
    self.charisma = charisma
    self.health = health
    self.lowAttack = lowAttack
    self.highAttack = highAttack
    self.attack = attack
    self.ability_points = ability_points
    self.characterName = ""

Here is what the data looks like when it is first received by the sever as a dict:以下是服务器首次将数据作为 dict 接收时的样子:

{'stats': ['strength', 'stamina', 'armor', 'agility', 'charisma'], 'strength': 1, 'stamina': 0, 'armor': 0, 'agility': 0, 'charisma': 0, 'health': 0, 'lowAttack': 0, 'highAttack': 0, 'attack': 0, 'ability_points': 15, 'characterName': 'Buck County'}

it may be hard to notice in this example, but this dicts values are different than the default values in the constructor for the Character class, for example:在此示例中可能很难注意到,但此 dicts 值与 Character class 的构造函数中的默认值不同,例如:

'strength': 1
'characterName': 'Buck County'
'ability_points': 15

Most importantly noted in the above is the 'characterName': 'Buck County'上面最重要的是'characterName': 'Buck County'

Ideally, what I would like to do next is turn this into an instance of a class in order to perform all the validations I've created and then just use instance.__dict__ to persist it.理想情况下,我接下来想做的是把它变成一个 class 的实例,以便执行我创建的所有验证,然后使用instance.__dict__来持久化它。

Right now, this is what i'm doing based on what i've read about unpacking:现在,这就是我正在做的基于我读到的关于拆包的内容:

newCharacter = Character(**character)
print(newCharacter.__dict__)

Here is what is printed from the above:这是上面打印的内容:

{'stats': ['strength', 'stamina', 'armor', 'agility', 'charisma'], 'strength': 1, 'stamina': 0, 'armor': 0, 'agility': 0, 'charisma': 0, 'health': 0, 'lowAttack': 0, 'highAttack': 0, 'attack': 0, 'ability_points': 15, 'characterName': ''}

So, below i'm going to put the print out of the dict that is sent to the server from the client vs the dict that belongs to the class once dict has been unpacked into .因此,在下面我将把从客户端发送到服务器字典与属于 class 的字典的打印打印出来,一旦字典被解压缩到.

{'stats': ['strength', 'stamina', 'armor', 'agility', 'charisma'], 'strength': 1, 'stamina': 0, 'armor': 0, 'agility': 0, 'charisma': 0, 'health': 0, 'lowAttack': 0, 'highAttack': 0, 'attack': 0, 'ability_points': 15, 'characterName': 'Buck County'}

{'stats': ['strength', 'stamina', 'armor', 'agility', 'charisma'], 'strength': 1, 'stamina': 0, 'armor': 0, 'agility': 0, 'charisma': 0, 'health': 0, 'lowAttack': 0, 'highAttack': 0, 'attack': 0, 'ability_points': 15, 'characterName': ''}

You can see in the comparison very clearly that the only value not being properly unpacked is the name.您可以在比较中非常清楚地看到,唯一未正确解包的值是名称。 Instead, it is resulting to the default value ""相反,它会导致默认值“”

I am new with Python and come from a Java / JS background.我是 Python 的新手,来自 Java / JS 背景。 Any suggestion would be appreciated.任何建议将不胜感激。 Thank you谢谢

I may be misunderstanding the issue, but it looks like in your __init__ , you define self.characterName = "" , not self.characterName = characterName我可能误解了这个问题,但看起来在你的__init__中,你定义self.characterName = "" ,而不是self.characterName = characterName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM