我正在学习Python编程语言,我遇到了一些我不完全理解的东西。
在如下方法中:
def method(self, blah):
def __init__(?):
....
....
self做什么?这意味着什么?这是强制性的吗?
__init__方法是做什么的?为什么有必要?(等)
我认为它们可能是面向对象结构,但我知道的不多。
我正在学习Python编程语言,我遇到了一些我不完全理解的东西。
在如下方法中:
def method(self, blah):
def __init__(?):
....
....
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是类实例的引用
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"
简而言之:
正如它所暗示的那样,Self指的是它自己——调用方法的对象。也就是说,如果你有N个对象调用这个方法,那么self。a将引用N个对象中每个对象的变量的一个单独实例。假设每个对象有N个变量a的副本 __init__在其他OOP语言(如c++ /Java)中被称为构造函数。基本思想是,它是一个特殊的方法,当创建该类的对象时自动调用
在这段代码中:
class A(object):
def __init__(self):
self.x = 'Hello'
def method_a(self, foo):
print self.x + ' ' + foo
... self变量表示对象本身的实例。大多数面向对象语言将此作为一个隐藏参数传递给对象上定义的方法;Python则不然。你必须显式地声明它。当你创建一个A类的实例并调用它的方法时,它将被自动传递,就像在…
a = A() # We do not pass any argument to the __init__ method
a.method_a('Sailor!') # We only pass a single argument
__init__方法大致代表Python中的构造函数。当你调用A()时,Python为你创建一个对象,并将其作为第一个参数传递给__init__方法。任何额外的参数(例如,A(24, 'Hello'))也会作为参数传递——在这种情况下会引发异常,因为构造函数并不期望它们。
是的,你是对的,这些是oop结构。
__init__是类的构造函数。self形参引用对象的实例(在c++中就像这样)。
class Point:
def __init__(self, x, y):
self._x = x
self._y = y
__init__方法在对象的内存被分配后被调用:
x = Point(1,2)
如果你想在对象中持久化这个值,在对象的方法中使用self参数是很重要的。例如,如果你像这样实现__init__方法:
class Point:
def __init__(self, x, y):
_x = x
_y = y
您的x和y参数将存储在堆栈上的变量中,当init方法超出作用域时将被丢弃。将这些变量设置为self。_x和self。_y将这些变量设置为Point对象的成员(在对象的生命周期内都可以访问)。
注意:对这个回答中“构造者”一词的使用做了一些澄清。从技术上讲,在Python中,“构造函数”的职责被划分为两个方法。这些方法是__new__(负责分配内存)和__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.