python 个对象从错误的文本文件中获取先前对象的属性,同时从正确的文本文件中获取它们自己的属性
python objects taking previous objects attributes from the wrong text files as well as their own from the right text files
我正在尝试将文本文件中的数据加载到 class 的 python 对象中。然而,当我 运行 代码时,每个对象都将从以前的文本文件中加载的所有数据加载到它的属性中,我不知道这是怎么发生的。非常感谢您的帮助,谢谢。
class Cocktail:
recipe = []
categories = []
line = ''
line2 = ''
character = []
name = ''
path = ''
def __init__(self):
cocktails.append(self)
#self.name = name
def define(self, name):
#print(self.name)
self.name = name #the name of the cocktail is taken from a master text file
self.path = self.name + '.txt' #each object will be linked to an individual text file containing its data
with open(self.path, 'r') as cocktail_data: #opening the specific text file
for line in cocktail_data:
self.character = line.split(' ', 1)
if self.character[0] == '#': #if the line starts with a #
self.line = line.strip('#')
self.line2 = self.line.strip('/n')
self.categories.append(self.line2) #loads that lines data into the objects 'categories list'
elif line.strip() == 'recipe': #when the recipe part of the text file is reached
break
for line in cocktail_data: #continuing through the document
self.recipe.append(line.strip()) #adds each line of the rest of the document to the object's 'recipe' attribute
def print(self): #for testing
print(self.name) #prints the name of that cocktail that the object represents
print(self.categories) #prints all the categories for that object
print(self.recipe) #prints the recipe list for that object
print('end')
#each new object seems to have the values (for its categories and recipe attributes) for all previous objects as well as its own
cocktails = []
with open('master.txt') as input_data:
for line in input_data:
name = line.strip() #the string that is the current line on the master text file
cocktail = Cocktail() #creates object of Cocktail for each name in master document
cocktail.define(name)
cocktail.print()
这是我的 python shell 显示:
cocktail1
[' a\n', ' b\n', ' c\n', ' d\n']
['one', 'two', 'three', 'end']
end
cocktail2
[' a\n', ' b\n', ' c\n', ' d\n', ' aq\n', ' bq\n', ' cq\n', ' dq\n']
['one', 'two', 'three', 'end', 'one q', 'two q', 'threeq', 'endq']
end
cocktail3
[' a\n', ' b\n', ' c\n', ' d\n', ' aq\n', ' bq\n', ' cq\n', ' dq\n', ' aw\n', ' bw\n', ' cw\n', ' dw\n']
['one', 'two', 'three', 'end', 'one q', 'two q', 'threeq', 'endq', 'onew', 'two w', 'threew', 'endw']
end
cocktail1 不应该有任何结尾带有 'q' 或 'w' 的,cocktail 2 应该只有结尾有 'q' 的,cocktail 3 应该只有结尾有 'w' 的
您可能已经习惯了 Java 或其他在 class 主体中声明实例属性的 OO 语言 - 而您正在尝试使用以下方法执行相同的操作:
class Cocktail:
recipe = []
categories = []
line = ''
line2 = ''
character = []
name = ''
path = ''
def __init__(self):
cocktails.append(self)
#self.name = name
然而,在 Python 中,class 主体中的任何声明都是针对 class 属性的。实例属性直接在实例上设置(内部方法使用 self.attrname = value
类型的语句)
对于上面的一些属性你不会看到问题,因为你使用不可变对象,并且当你在实例中设置它们时只是隐藏 class 属性 - 就像你做 self.name = name
- 您单独在该实例上创建了一个新的 name
属性。
但是,当您执行 self.categories.append(...)
时,您正在修改在 cocktail class 中定义的单个 categories
对象(列表),所有 cocktail 对象都可以访问该对象。
要修复您的行为,只需声明您的实例属性 - 这样做,例如:
class Cocktail:
def __init__(self):
self.recipe = []
self.categories = []
self.line = ''
self.line2 = ''
self.character = []
self.name = ''
self.path = ''
(但请记住,在需要在运行时设置其值之前,您不必在 Python 中声明变量或属性 - 您只需在需要的方法中设置它们 -只有您真正需要创建的是包含列表的那些,因为您要向这些列表附加值)。
我正在尝试将文本文件中的数据加载到 class 的 python 对象中。然而,当我 运行 代码时,每个对象都将从以前的文本文件中加载的所有数据加载到它的属性中,我不知道这是怎么发生的。非常感谢您的帮助,谢谢。
class Cocktail:
recipe = []
categories = []
line = ''
line2 = ''
character = []
name = ''
path = ''
def __init__(self):
cocktails.append(self)
#self.name = name
def define(self, name):
#print(self.name)
self.name = name #the name of the cocktail is taken from a master text file
self.path = self.name + '.txt' #each object will be linked to an individual text file containing its data
with open(self.path, 'r') as cocktail_data: #opening the specific text file
for line in cocktail_data:
self.character = line.split(' ', 1)
if self.character[0] == '#': #if the line starts with a #
self.line = line.strip('#')
self.line2 = self.line.strip('/n')
self.categories.append(self.line2) #loads that lines data into the objects 'categories list'
elif line.strip() == 'recipe': #when the recipe part of the text file is reached
break
for line in cocktail_data: #continuing through the document
self.recipe.append(line.strip()) #adds each line of the rest of the document to the object's 'recipe' attribute
def print(self): #for testing
print(self.name) #prints the name of that cocktail that the object represents
print(self.categories) #prints all the categories for that object
print(self.recipe) #prints the recipe list for that object
print('end')
#each new object seems to have the values (for its categories and recipe attributes) for all previous objects as well as its own
cocktails = []
with open('master.txt') as input_data:
for line in input_data:
name = line.strip() #the string that is the current line on the master text file
cocktail = Cocktail() #creates object of Cocktail for each name in master document
cocktail.define(name)
cocktail.print()
这是我的 python shell 显示:
cocktail1
[' a\n', ' b\n', ' c\n', ' d\n']
['one', 'two', 'three', 'end']
end
cocktail2
[' a\n', ' b\n', ' c\n', ' d\n', ' aq\n', ' bq\n', ' cq\n', ' dq\n']
['one', 'two', 'three', 'end', 'one q', 'two q', 'threeq', 'endq']
end
cocktail3
[' a\n', ' b\n', ' c\n', ' d\n', ' aq\n', ' bq\n', ' cq\n', ' dq\n', ' aw\n', ' bw\n', ' cw\n', ' dw\n']
['one', 'two', 'three', 'end', 'one q', 'two q', 'threeq', 'endq', 'onew', 'two w', 'threew', 'endw']
end
cocktail1 不应该有任何结尾带有 'q' 或 'w' 的,cocktail 2 应该只有结尾有 'q' 的,cocktail 3 应该只有结尾有 'w' 的
您可能已经习惯了 Java 或其他在 class 主体中声明实例属性的 OO 语言 - 而您正在尝试使用以下方法执行相同的操作:
class Cocktail:
recipe = []
categories = []
line = ''
line2 = ''
character = []
name = ''
path = ''
def __init__(self):
cocktails.append(self)
#self.name = name
然而,在 Python 中,class 主体中的任何声明都是针对 class 属性的。实例属性直接在实例上设置(内部方法使用 self.attrname = value
类型的语句)
对于上面的一些属性你不会看到问题,因为你使用不可变对象,并且当你在实例中设置它们时只是隐藏 class 属性 - 就像你做 self.name = name
- 您单独在该实例上创建了一个新的 name
属性。
但是,当您执行 self.categories.append(...)
时,您正在修改在 cocktail class 中定义的单个 categories
对象(列表),所有 cocktail 对象都可以访问该对象。
要修复您的行为,只需声明您的实例属性 - 这样做,例如:
class Cocktail:
def __init__(self):
self.recipe = []
self.categories = []
self.line = ''
self.line2 = ''
self.character = []
self.name = ''
self.path = ''
(但请记住,在需要在运行时设置其值之前,您不必在 Python 中声明变量或属性 - 您只需在需要的方法中设置它们 -只有您真正需要创建的是包含列表的那些,因为您要向这些列表附加值)。