如何将对象倾向的字符串转换为对象?

时间:2019-05-18 13:21:02

标签: python python-2.7

我有一个对象实例的字符串。有没有办法将其重新变成对象?

我尝试过ast.literal_eval,但是由于该对象是我自己编写的类,因此似乎无法正常工作。

课程:

class Card(pygame.sprite.Sprite):
    def __init__(self, card_name, player_class):
        """
        class card attributes:
        1) card name
        2) card profile image
        3) card image
        4) card type
        5) card rarity
        """

        super(self.__class__, self).__init__()

        self.card_name = card_name
        try:
            self.card_profile_img = pygame.image.load(card_name + '.png')
        except pygame.error:
            self.card_profile_img = pygame.image.load('beta_pic.png')

        if player_class == 'ironclad':
            with open('ironclad_cards.txt', 'r') as f:
                cards = f.read().split('\n')
                for card in cards:
                    if card.split('$')[0] == card_name:
                        self.card_type = card.split('$')[1]
                        self.card_rarity = card.split('$')[2]
                        self.card_img = pygame.image.load(self.card_rarity + '_' + self.card_type + '.png')
                        self.energy_cost = int(card.split('$')[3])
                        self.card_des = card.split('$')[4]
                        self.card_effect = card.split('$')[5]

        elif player_class == 'silent':
            with open('silent_cards.txt', 'r') as f:
                cards = f.read().split('\n')
                for card in cards:
                    if card.split('$')[0] == card_name:
                        self.card_type = card.split('$')[1]
                        self.card_rarity = card.split('$')[2]
                        self.card_img = pygame.image.load(self.card_rarity + '_' + self.card_type + '.png')
                        self.energy_cost = card.split('$')[3]
                        self.card_des = card.split('$')[4]
                        self.card_effect = card.split('$')[5]

        elif player_class == 'defect':
            with open('defect_cards.txt', 'r') as f:
                cards = f.read().split('\n')
                for card in cards:
                    if card.split('$')[0] == card_name:
                        self.card_type = card.split('$')[1]
                        self.card_rarity = card.split('$')[2]
                        self.card_img = pygame.image.load(self.card_rarity + '_' + self.card_type + '.png')
                        self.energy_cost = card.split('$')[3]
                        self.card_des = card.split('$')[4]
                        self.card_effect = card.split('$')[5]

        self.card_img_rect = self.card_img.get_rect()
        self.card_img_rect.x = 0
        self.card_img_rect.y = 0

我有:

<card.Card instance at 0x02AE7AA8>

1 个答案:

答案 0 :(得分:0)

您可以使用gc模块(受this answer启发)从该字符串中提取对象ID并通过ID查找该对象:

import gc

def object_by_str(string):
    object_id_str = s.split()[-1][:-1]  # maybe regex is better 
    object_id = int(object_id_str, 16)
    for obj in gc.get_objects():
        if id(obj) == object_id:
            return obj

class A:
    pass

a = A()
s = str(a)
print(s)
b = object_by_str(s)
print(a == b)

输出:

<__main__.A instance at 0x7f155dea4fc8>
True