我正在学习Python编程语言,我遇到了一些我不完全理解的东西。
在如下方法中:
def method(self, blah):
def __init__(?):
....
....
self做什么?这意味着什么?这是强制性的吗?
__init__方法是做什么的?为什么有必要?(等)
我认为它们可能是面向对象结构,但我知道的不多。
我正在学习Python编程语言,我遇到了一些我不完全理解的东西。
在如下方法中:
def method(self, blah):
def __init__(?):
....
....
self做什么?这意味着什么?这是强制性的吗?
__init__方法是做什么的?为什么有必要?(等)
我认为它们可能是面向对象结构,但我知道的不多。
当前回答
注意,self实际上可以是任何有效的python标识符。例如,从Chris B的例子中,我们可以简单地写:
class A(object):
def __init__(foo):
foo.x = 'Hello'
def method_a(bar, foo):
print bar.x + ' ' + foo
结果是一样的。然而,建议使用self,因为其他python更容易识别它。
其他回答
self是类实例的引用
class foo:
def bar(self):
print "hi"
现在我们可以创建一个foo实例并调用它的方法,self参数在本例中由Python添加:
f = foo()
f.bar()
但是如果方法调用不在类实例的上下文中,它也可以被传入,下面的代码做同样的事情
f = foo()
foo.bar(f)
有趣的是,变量名“self”只是一种约定。下面的定义将工作完全相同..说了这么多,这是一种非常强烈的惯例,应该一直遵循,但它确实说明了语言的灵活性
class foo:
def bar(s):
print "hi"
在这里,这个家伙写得很好,很简单: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.
试试这段代码。希望它能帮助很多像我一样的C程序员学习Py。
#! /usr/bin/python2
class Person:
'''Doc - Inside Class '''
def __init__(self, name):
'''Doc - __init__ Constructor'''
self.n_name = name
def show(self, n1, n2):
'''Doc - Inside Show'''
print self.n_name
print 'Sum = ', (n1 + n2)
def __del__(self):
print 'Destructor Deleting object - ', self.n_name
p=Person('Jay')
p.show(2, 3)
print p.__doc__
print p.__init__.__doc__
print p.show.__doc__
输出:
Jay
Sum = 5
博士-内部类
Doc - __init__构造函数
医生-内情秀
析构函数删除对象- Jay
self做什么?这意味着什么?这是强制性的吗?
每个类方法(包括init)的第一个参数始终是对类当前实例的引用。按照惯例,这个参数总是命名为self。在init方法中,self指向新创建的对象;在其他类方法中,它引用被调用方法的实例。
Python不会强制你使用"self"。你可以给它起任何你想要的名字。但是记住方法定义中的第一个参数是对对象的引用。Python为你将self参数添加到列表中;在调用方法时不需要包含它。 如果你没有在init方法中提供self,那么你会得到一个错误
TypeError: __init___() takes no arguments (1 given)
init方法做什么?为什么有必要?(等)
Init是initialization的缩写。它是一个构造函数,当你创建类的实例时调用它,这是不必要的。但通常我们的做法是编写init方法来设置对象的默认状态。如果你不愿意在初始时设置对象的任何状态,那么你不需要编写这个方法。
一个简单的说明性例子
希望它能有所帮助,这里有一个简单的例子,我用它来理解在类中声明的变量和在__init__函数中声明的变量之间的区别:
class MyClass(object):
i = 123
def __init__(self):
self.i = 345
a = MyClass()
print(a.i)
print(MyClass.i)
输出:
345
123