我创建了一个名为Hand的类和一个对象h1。我用代码将对象的结果写在文件(handtest.txt)中:
h1 = Hand(3)
datafile = open('handtest.txt', 'w')
datafile.write(str(h1))
datafile.close()
我想回读文件中的内容,并将其另存为另一个对象。但是,当我在文本文件中写入对象时,我已经将格式更改为字符串。如何再次将其另存为对象?是否可以在不使用酸洗和json的情况下这样做? 这是我的手形课:
from card import Card
import random
"""
Defines class Hand, in file hand.py
"""
class Hand:
"""
One object of class Hand represents a hand of cards
and so one object stores a number of Card objects.
"""
def __init__(self, numCardsInHand):
"""
Initializes a Hand object with numCardsInHand Card objects inside it.
"""
# Creates a list for storing a hand of cards.
self.list = []
# Creates a list for storing bj value.
self.listBJ = []
# Uses a loop to store a hand of cards to a list and stores the bj values of the cards to another list.
for count in range(numCardsInHand):
#call hitMe()
Hand.hitMe(self)
def bjValue(self):
"""
Returns the blackjack value for the whole Hand of cards
"""
BJ = ""
# Sums up all the bj values in self.listBJ and returns it to the bjValue method.
BJ = sum(self.listBJ)
return BJ
def __str__(self):
"""
Calls the __str__( ) method and uses the returned value
from the __str__( ) method as the string that is required.
Returns the local variable in the string method.
"""
# Tranforms the self.list from list type to string type and saves it as a local variable.
all_items = '\n'.join(map(str, self.list))
return all_items
def hitMe(self):
"""
Adds one randomly generated Card to the Hand.
Randomly chooses a rank number and a suit type and save them as local variables.
Uses the variables as the Card argument in an object. Then append the object to the self.list
"""
# Randomly chooses a number from 1-13 and stores it as a local variable.
all_rank = random.randint(1, 13)
# Lists suit types.
random_suit = ['d', 'c', 'h', 's']
# Randomly chooses a suit type in the random_suit list and stores it as a local variable.
all_suit = random.choice(random_suit)
# Uses the local variables as arguments of one object of the class Card and appends the object to the self.list.
self.list.append(Card(all_rank, all_suit))
# Appends all_rank to self.listBJ.
self.listBJ.append(all_rank)
答案 0 :(得分:0)
Python中的许多对象都有一个repr()
,它与最初用于创建它的Python代码相对应。因此,首先将行更改为使用repr
而不是str
。 (尽管对于某些对象,__str__
方法仅调用__repr__
。)
datafile.write(repr(h1))
您可以使用类似的repr通过在对象上使用eval()
来重建对象。这不像使用pickle那样可靠,因为某些代表不是有效的Python代码。
with open('handtest.txt') as datafile:
h1 = eval(datafile.read())
请注意,这是安全隐患。不要读取和eval()
不信任的文件的内容,因为恶意的Python代码可能会做坏的事情,例如删除所有文件。
当然,这仅在repr是有效的Python代码时才有效。您可能必须为手课编写一个正确的__repr__
方法。另外,有时这需要适当的导入工作。例如,如果您要eval
键入字符串'Hand(3)'
,则必须导入Hand
才能使它起作用。
评估repr仅在可以在单个表达式中设置整个对象状态的情况下才有效。当前,您的课程尚未设计成这样做,因为它使__init__
中的内容随机化。因此,有效的repr表达式虽然可能,但会很尴尬。
这不必阻止您。您可以使用@classmethod
在Python中创建备用初始化程序。创建一个可以创建实例并设置其所有状态的实例。也许像
@classmethod
def of(cls, hand, bj):
hand = super().__new__(cls)
hand.list = hand
hand.listBj = bj
return hand
然后,您可以为此创建一个不错的代表。
def __repr__(self):
return f"Hand.of(hand={self.list!r}, bj={self.listBj!r})
如果要保留更多状态,请将其添加为类方法的另一个参数,然后在代表中显示。