I'm writing some code that takes a filename, opens the file, and parses out some data. I'd like to do this in a class. The following code works:
我正在编写一些带有文件名的代码,打开文件并解析一些数据。我想在课堂上这样做。以下代码有效:
class MyClass():
def __init__(self, filename):
self.filename = filename
self.stat1 = None
self.stat2 = None
self.stat3 = None
self.stat4 = None
self.stat5 = None
def parse_file():
#do some parsing
self.stat1 = result_from_parse1
self.stat2 = result_from_parse2
self.stat3 = result_from_parse3
self.stat4 = result_from_parse4
self.stat5 = result_from_parse5
parse_file()
But it involves me putting all of the parsing machinery in the scope of the __init__
function for my class. That looks fine now for this simplified code, but the function parse_file
has quite a few levels of indention as well. I'd prefer to define the function parse_file()
as a class function like below:
但它涉及我将所有解析机制放在我班级__init__函数的范围内。对于这个简化的代码,现在看起来很好,但是函数parse_file也有很多级别的缩进。我更喜欢将函数parse_file()定义为类函数,如下所示:
class MyClass():
def __init__(self, filename):
self.filename = filename
self.stat1 = None
self.stat2 = None
self.stat3 = None
self.stat4 = None
self.stat5 = None
parse_file()
def parse_file():
#do some parsing
self.stat1 = result_from_parse1
self.stat2 = result_from_parse2
self.stat3 = result_from_parse3
self.stat4 = result_from_parse4
self.stat5 = result_from_parse5
Of course this code doesn't work because the function parse_file()
is not within the scope of the __init__
function. Is there a way to call a class function from within __init__
of that class? Or am I thinking about this the wrong way?
当然这段代码不起作用,因为函数parse_file()不在__init__函数的范围内。有没有办法从该类的__init__中调用类函数?或者我是否以错误的方式思考这个问题?
6 个解决方案
#1
93
Call the function in this way:
以这种方式调用函数:
self.parse_file()
You also need to define your parse_file() function like this:
您还需要像这样定义parse_file()函数:
def parse_file(self):
The parse_file
method has to be bound to an object upon calling it (because it's not a static method). This is done by calling the function on an instance of the object, in your case the instance is self
.
parse_file方法必须在调用它时绑定到对象(因为它不是静态方法)。这是通过在对象的实例上调用函数来完成的,在这种情况下,实例是self。
#2
17
If I'm not wrong, both functions are part of your class, you should use it like this:
如果我没错,这两个函数都是你班级的一部分,你应该像这样使用它:
class MyClass():
def __init__(self, filename):
self.filename = filename
self.stat1 = None
self.stat2 = None
self.stat3 = None
self.stat4 = None
self.stat5 = None
self.parse_file()
def parse_file(self):
#do some parsing
self.stat1 = result_from_parse1
self.stat2 = result_from_parse2
self.stat3 = result_from_parse3
self.stat4 = result_from_parse4
self.stat5 = result_from_parse5
replace your line:
替换你的行:
parse_file()
with:
有:
self.parse_file()
#3
5
How about:
怎么样:
class MyClass(object):
def __init__(self, filename):
self.filename = filename
self.stats = parse_file(filename)
def parse_file(filename):
#do some parsing
return results_from_parse
By the way, if you have variables named stat1
, stat2
, etc., the situation is begging for a tuple: stats = (...)
.
顺便说一句,如果你有名为stat1,stat2等的变量,那么情况就是要求元组:stats =(...)。
So let parse_file
return a tuple, and store the tuple in self.stats
.
所以让parse_file返回一个元组,并将元组存储在self.stats中。
Then, for example, you can access what used to be called stat3
with self.stats[2]
.
然后,例如,您可以使用self.stats [2]访问曾经被称为stat3的内容。
#4
0
In parse_file
, take the self
argument (just like in __init__
). If there's any other context you need then just pass it as additional arguments as usual.
在parse_file中,使用self参数(就像在__init__中一样)。如果您需要任何其他上下文,那么只需像往常一样将其作为附加参数传递。
#5
0
You must declare parse_file like this; def parse_file(self)
. The "self" parameter is a hidden parameter in most languages, but not in python. You must add it to the definition of all that methods that belong to a class. Then you can call the function from any method inside the class using self.parse_file
你必须像这样声明parse_file; def parse_file(self)。 “self”参数是大多数语言中的隐藏参数,但不是python中的隐藏参数。您必须将其添加到属于类的所有方法的定义中。然后,您可以使用self.parse_file从类中的任何方法调用该函数
your final program is going to look like this:
你的最终节目将如下所示:
class MyClass():
def __init__(self, filename):
self.filename = filename
self.stat1 = None
self.stat2 = None
self.stat3 = None
self.stat4 = None
self.stat5 = None
self.parse_file()
def parse_file(self):
#do some parsing
self.stat1 = result_from_parse1
self.stat2 = result_from_parse2
self.stat3 = result_from_parse3
self.stat4 = result_from_parse4
self.stat5 = result_from_parse5
#6
-9
I think that your problem is actually with not correctly indenting init function.It should be like this
我认为你的问题实际上是没有正确缩进init函数。它应该是这样的
class MyClass():
def __init__(self, filename):
pass
def parse_file():
pass
#1
93
Call the function in this way:
以这种方式调用函数:
self.parse_file()
You also need to define your parse_file() function like this:
您还需要像这样定义parse_file()函数:
def parse_file(self):
The parse_file
method has to be bound to an object upon calling it (because it's not a static method). This is done by calling the function on an instance of the object, in your case the instance is self
.
parse_file方法必须在调用它时绑定到对象(因为它不是静态方法)。这是通过在对象的实例上调用函数来完成的,在这种情况下,实例是self。
#2
17
If I'm not wrong, both functions are part of your class, you should use it like this:
如果我没错,这两个函数都是你班级的一部分,你应该像这样使用它:
class MyClass():
def __init__(self, filename):
self.filename = filename
self.stat1 = None
self.stat2 = None
self.stat3 = None
self.stat4 = None
self.stat5 = None
self.parse_file()
def parse_file(self):
#do some parsing
self.stat1 = result_from_parse1
self.stat2 = result_from_parse2
self.stat3 = result_from_parse3
self.stat4 = result_from_parse4
self.stat5 = result_from_parse5
replace your line:
替换你的行:
parse_file()
with:
有:
self.parse_file()
#3
5
How about:
怎么样:
class MyClass(object):
def __init__(self, filename):
self.filename = filename
self.stats = parse_file(filename)
def parse_file(filename):
#do some parsing
return results_from_parse
By the way, if you have variables named stat1
, stat2
, etc., the situation is begging for a tuple: stats = (...)
.
顺便说一句,如果你有名为stat1,stat2等的变量,那么情况就是要求元组:stats =(...)。
So let parse_file
return a tuple, and store the tuple in self.stats
.
所以让parse_file返回一个元组,并将元组存储在self.stats中。
Then, for example, you can access what used to be called stat3
with self.stats[2]
.
然后,例如,您可以使用self.stats [2]访问曾经被称为stat3的内容。
#4
0
In parse_file
, take the self
argument (just like in __init__
). If there's any other context you need then just pass it as additional arguments as usual.
在parse_file中,使用self参数(就像在__init__中一样)。如果您需要任何其他上下文,那么只需像往常一样将其作为附加参数传递。
#5
0
You must declare parse_file like this; def parse_file(self)
. The "self" parameter is a hidden parameter in most languages, but not in python. You must add it to the definition of all that methods that belong to a class. Then you can call the function from any method inside the class using self.parse_file
你必须像这样声明parse_file; def parse_file(self)。 “self”参数是大多数语言中的隐藏参数,但不是python中的隐藏参数。您必须将其添加到属于类的所有方法的定义中。然后,您可以使用self.parse_file从类中的任何方法调用该函数
your final program is going to look like this:
你的最终节目将如下所示:
class MyClass():
def __init__(self, filename):
self.filename = filename
self.stat1 = None
self.stat2 = None
self.stat3 = None
self.stat4 = None
self.stat5 = None
self.parse_file()
def parse_file(self):
#do some parsing
self.stat1 = result_from_parse1
self.stat2 = result_from_parse2
self.stat3 = result_from_parse3
self.stat4 = result_from_parse4
self.stat5 = result_from_parse5
#6
-9
I think that your problem is actually with not correctly indenting init function.It should be like this
我认为你的问题实际上是没有正确缩进init函数。它应该是这样的
class MyClass():
def __init__(self, filename):
pass
def parse_file():
pass