[英]The method is not called inside a class instance
Can't access read_file()
method inside an instance class in Python 3.7.无法在 Python 3.7 的实例类中访问
read_file()
方法。 I want to print the list
list_values_end with functions
print_file() . When calling the function
我想
with functions
print_file() 打印list
list_values_end . When calling the function
. When calling the function
print_file () the terminal produces
<__ main __. . When calling the function
print_file() 时the terminal produces
<__ main __。 ReadFile object at 0x0082EA70>` instead of the expected list. ReadFile 对象位于 0x0082EA70>` 而不是预期的列表。 Please explain what was done wrong?
请解释做错了什么? The code is attached.
附上代码。
class ReadFile():
"""Чтение из файла"""
def __init__(self):
self.file_r = 'val_kWt.txt'
self.read_file(self.file_r)
self.print_file(self)
def read_file(self, file_r):
list_values_end = []
vk = open(file_r, 'r')
list_values = vk.readlines()
for list_value in list_values:
list_values_end.append(list_value.rstrip())
vk.close()
print(list_values_end)
return list_values_end
def print_file(self, list_values_end):
print(list_values_end)
examp = ReadFile()示例 = ReadFile()
You have to place print
before return
statement.您必须在
return
语句之前放置print
。 Try this instead:试试这个:
class ReadFile():
def __init__(self):
self.file_r = 'val_kWt.txt'
self.read_file(self.file_r)
self.a = 12
self.print_a(self.a)
def print_a(self, a):
print('Printing ', a)
def read_file(self, file_r):
list_values_end = []
vk = open(file_r, 'r')
list_values = vk.readlines()
for list_value in list_values:
list_values_end.append(list_value.rstrip())
vk.close()
print(list_values_end)
return list_values_end
Edited according to question modification根据问题修改编辑
You passed the object to print_file
function.您将对象传递给了
print_file
函数。 Instead you should have passed the return value of read_file相反,您应该传递 read_file 的返回值
class ReadFile():
"""Чтение из файла"""
def __init__(self):
self.file_r = 'val_kWt.txt'
list_value_end = self.read_file(self.file_r)
self.print_file(list_value_end)
def read_file(self, file_r):
list_values_end = []
vk = open(file_r, 'r')
list_values = vk.readlines()
for list_value in list_values:
list_values_end.append(list_value.rstrip())
vk.close()
print(list_values_end)
return list_values_end
def print_file(self, list_values_end):
print(list_values_end)
examp = ReadFile()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.