没有'self'的Python调用方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18679803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python calling method without 'self'
提问by Synaps3
So I just started programming in python and I don't understand the whole reasoning behind 'self'. I understand that it is used almost like a global variable, so that data can be passed between different methods in the class. I don't understand why you need to use it when your calling another method in the same class. If I am already in that class, why do I have to tell it??
所以我刚开始用 python 编程,我不明白“自我”背后的全部推理。我知道它几乎像一个全局变量一样使用,因此可以在类中的不同方法之间传递数据。我不明白为什么在调用同一个类中的另一个方法时需要使用它。如果我已经在那个班级,我为什么要告诉它?
example, if I have: Why do I need self.thing()?
例如,如果我有:为什么我需要 self.thing()?
class bla:
def hello(self):
self.thing()
def thing(self):
print "hello"
回答by Bleeding Fingers
One reason is to refer to the method of that particularclass's instance within which the code is be executed.
一个原因是引用在其中执行代码的特定类的实例的方法。
This example might help:
这个例子可能有帮助:
def hello():
print "global hello"
class bla:
def hello(self):
self.thing()
hello()
def thing(self):
print "hello"
b = bla()
b.hello()
>>> hello
global hello
You can think of it, for now, to be namespace resolution.
现在,您可以将其视为名称空间解析。
回答by D4NI3LS
There might be another function with the same name outside your class.
self
is an object reference to the object itself, therefore, they are same.
Python methods are not called in the context of the object itself. self
in Python may be used to deal with custom object models or something.
在您的班级之外可能还有另一个同名的函数。
self
是对对象本身的对象引用,因此,它们是相同的。Python 方法不在对象本身的上下文中调用。self
在 Python 中可以用来处理自定义对象模型什么的。
回答by Ali Alkhatib
The short answer is "because you can def thing(args)
as a global function, or as a method of another class. Take this (horrible) example:
简短的回答是“因为你可以def thing(args)
作为一个全局函数,或者作为另一个类的方法。以这个(可怕的)例子为例:
def thing(args):
print "Please don't do this."
class foo:
def thing(self,args):
print "No, really. Don't ever do this."
class bar:
def thing(self,args):
print "This is completely unrelated."
This is bad. Don't do this. But if you did, you could call thing(args)
and somethingwould happen. This can potentially be a good thing if you plan accordingly:
这不好。不要这样做。但是如果你这样做了,你可以打电话thing(args)
,事情就会发生。如果您有相应的计划,这可能是一件好事:
class Person:
def bio(self):
print "I'm a person!"
class Student(Person):
def bio(self):
Person.bio(self)
print "I'm studying %s" % self.major
The above code makes it so that if you create an object of Student
class and call bio
, it'll do all the stuff that would have happened if it was of Person
class that had its own bio
called andit'll do its own thing afterwards.
上面的代码使得如果你创建一个Student
class 和 call的对象bio
,它会做所有会发生的事情,如果它是Person
自己bio
被调用的类,然后它会做自己的事情。
This gets into inheritance and some other stuff you might not have seen yet, but look forward to it.
这涉及到继承和其他一些你可能还没有见过的东西,但请期待它。
回答by Developer
Also you can make methods in class static
so no need for self
. However, use this if you really need that.
你也可以在课堂上创建方法,static
所以不需要self
. 但是,如果您确实需要它,请使用它。
Yours:
你的:
class bla:
def hello(self):
self.thing()
def thing(self):
print "hello"
static edition:
静态版:
class bla:
@staticmethod
def hello():
bla.thing()
@staticmethod
def thing():
print "hello"
回答by frankwang4strangers
To me, self like a scope definer, with self.foo() and self.bar indicating the function and the parameter defined in the class and not those defines in the other places.
对我来说,self 就像一个范围定义器,self.foo() 和 self.bar 表示在类中定义的函数和参数,而不是在其他地方定义的那些。
回答by BrainAtom
A mirror reflects the difference with java and python: java could use method within class without self because the only way to call a method is from class or obj, so the way is clear to compiler. While this may confuse the translator of python and consumes significant time to look up the symbol table over the whole name space in running time, because both function and class method are the potential candidates for a function call given the 'self' is not used. If this practice is used, the python speed would once again slowed down compared to C and Java which could make it less attractive.
一个镜像反映了java和python的区别:java可以在没有self的情况下使用类内的方法,因为调用方法的唯一方法是从类或obj,所以编译器很清楚。虽然这可能会混淆 python 的翻译器并在运行时消耗大量时间在整个名称空间中查找符号表,因为函数和类方法都是函数调用的潜在候选者,因为没有使用“self”。如果使用这种做法,与 C 和 Java 相比,python 的速度将再次变慢,这可能会降低其吸引力。
回答by karthi
I have tried below code that declared method with out parameter in class and called method using class name.
我试过下面的代码,在类中声明了带有 out 参数的方法,并使用类名调用了方法。
class Employee:
def EmpWithOutPar():
return 'Hi you called Employee'
print(Employee.EmpWithOutPar())
output : Hi you called Employee
输出:嗨,你叫员工