我正在学习Python编程语言,我遇到了一些我不完全理解的东西。
在如下方法中:
def method(self, blah):
def __init__(?):
....
....
self做什么?这意味着什么?这是强制性的吗?
__init__方法是做什么的?为什么有必要?(等)
我认为它们可能是面向对象结构,但我知道的不多。
我正在学习Python编程语言,我遇到了一些我不完全理解的东西。
在如下方法中:
def method(self, blah):
def __init__(?):
....
....
self做什么?这意味着什么?这是强制性的吗?
__init__方法是做什么的?为什么有必要?(等)
我认为它们可能是面向对象结构,但我知道的不多。
当前回答
在这里,这个家伙写得很好,很简单:https://www.jeffknupp.com/blog/2014/06/18/improve-your-python-python-classes-and-object-oriented-programming/
阅读上面的链接作为参考:
self? So what's with that self parameter to all of the Customer methods? What is it? Why, it's the instance, of course! Put another way, a method like withdraw defines the instructions for withdrawing money from some abstract customer's account. Calling jeff.withdraw(100.0) puts those instructions to use on the jeff instance. So when we say def withdraw(self, amount):, we're saying, "here's how you withdraw money from a Customer object (which we'll call self) and a dollar figure (which we'll call amount). self is the instance of the Customer that withdraw is being called on. That's not me making analogies, either. jeff.withdraw(100.0) is just shorthand for Customer.withdraw(jeff, 100.0), which is perfectly valid (if not often seen) code. init self may make sense for other methods, but what about init? When we call init, we're in the process of creating an object, so how can there already be a self? Python allows us to extend the self pattern to when objects are constructed as well, even though it doesn't exactly fit. Just imagine that jeff = Customer('Jeff Knupp', 1000.0) is the same as calling jeff = Customer(jeff, 'Jeff Knupp', 1000.0); the jeff that's passed in is also made the result. This is why when we call init, we initialize objects by saying things like self.name = name. Remember, since self is the instance, this is equivalent to saying jeff.name = name, which is the same as jeff.name = 'Jeff Knupp. Similarly, self.balance = balance is the same as jeff.balance = 1000.0. After these two lines, we consider the Customer object "initialized" and ready for use. Be careful what you __init__ After init has finished, the caller can rightly assume that the object is ready to use. That is, after jeff = Customer('Jeff Knupp', 1000.0), we can start making deposit and withdraw calls on jeff; jeff is a fully-initialized object.
其他回答
在这里,这个家伙写得很好,很简单:https://www.jeffknupp.com/blog/2014/06/18/improve-your-python-python-classes-and-object-oriented-programming/
阅读上面的链接作为参考:
self? So what's with that self parameter to all of the Customer methods? What is it? Why, it's the instance, of course! Put another way, a method like withdraw defines the instructions for withdrawing money from some abstract customer's account. Calling jeff.withdraw(100.0) puts those instructions to use on the jeff instance. So when we say def withdraw(self, amount):, we're saying, "here's how you withdraw money from a Customer object (which we'll call self) and a dollar figure (which we'll call amount). self is the instance of the Customer that withdraw is being called on. That's not me making analogies, either. jeff.withdraw(100.0) is just shorthand for Customer.withdraw(jeff, 100.0), which is perfectly valid (if not often seen) code. init self may make sense for other methods, but what about init? When we call init, we're in the process of creating an object, so how can there already be a self? Python allows us to extend the self pattern to when objects are constructed as well, even though it doesn't exactly fit. Just imagine that jeff = Customer('Jeff Knupp', 1000.0) is the same as calling jeff = Customer(jeff, 'Jeff Knupp', 1000.0); the jeff that's passed in is also made the result. This is why when we call init, we initialize objects by saying things like self.name = name. Remember, since self is the instance, this is equivalent to saying jeff.name = name, which is the same as jeff.name = 'Jeff Knupp. Similarly, self.balance = balance is the same as jeff.balance = 1000.0. After these two lines, we consider the Customer object "initialized" and ready for use. Be careful what you __init__ After init has finished, the caller can rightly assume that the object is ready to use. That is, after jeff = Customer('Jeff Knupp', 1000.0), we can start making deposit and withdraw calls on jeff; jeff is a fully-initialized object.
基本上,当在同一个类中的多个函数中使用一个变量时,需要使用'self'关键字。至于init,它用于设置默认值,以防该类中没有其他函数被调用。
在这段代码中:
class Cat:
def __init__(self, name):
self.name = name
def info(self):
print 'I am a cat and I am called', self.name
在这里,__init__充当类的构造函数,当对象实例化时,将调用此函数。Self表示实例化对象。
c = Cat('Kitty')
c.info()
上述陈述的结果如下:
I am a cat and I am called Kitty
注意,self实际上可以是任何有效的python标识符。例如,从Chris B的例子中,我们可以简单地写:
class A(object):
def __init__(foo):
foo.x = 'Hello'
def method_a(bar, foo):
print bar.x + ' ' + foo
结果是一样的。然而,建议使用self,因为其他python更容易识别它。
Python __init__和self它们做什么? self做什么?这意味着什么?这是强制性的吗? __init__方法是做什么的?为什么有必要?(等)
给出的例子是不正确的,所以让我基于它创建一个正确的例子:
class SomeObject(object):
def __init__(self, blah):
self.blah = blah
def method(self):
return self.blah
当我们创建对象的实例时,会在对象创建后调用__init__来自定义对象。也就是说,当我们在下面用'blah'(可以是任何东西)调用SomeObject时,它会作为参数传递给__init__函数,blah:
an_object = SomeObject('blah')
self参数是SomeObject的实例,它将被赋值给an_object。
稍后,我们可能想在这个对象上调用一个方法:
an_object.method()
执行点查找,即an_object。方法,将实例绑定到函数的一个实例,并且该方法(如上所述)现在是一个“绑定”方法——这意味着我们不需要显式地将实例传递给方法调用。
方法调用获取实例,因为它绑定在点查找上,当调用时,执行它被编程执行的任何代码。
隐式传递的self参数按照约定称为self。我们可以使用任何其他合法的Python名称,但如果您将其更改为其他名称,则可能会受到其他Python程序员的批评。
__init__是一个特殊的方法,在Python数据模型文档中有说明。它在实例创建后立即被调用(通常通过__new__ -尽管__new__不是必需的,除非你要继承一个不可变的数据类型)。