Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
307 views
in Technique[技术] by (71.8m points)

class - 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. 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.

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.

Character has a constructor like so:

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:

{'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:

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

Most importantly noted in the above is the '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.

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.

{'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. Any suggestion would be appreciated. Thank you


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I may be misunderstanding the issue, but it looks like in your __init__, you define self.characterName = "", not self.characterName = characterName


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...