我正在学习Python编程语言,我遇到了一些我不完全理解的东西。

在如下方法中:

def method(self, blah):
    def __init__(?):
        ....
    ....

self做什么?这意味着什么?这是强制性的吗?

__init__方法是做什么的?为什么有必要?(等)

我认为它们可能是面向对象结构,但我知道的不多。


当前回答

试试这段代码。希望它能帮助很多像我一样的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实际上可以是任何有效的python标识符。例如,从Chris B的例子中,我们可以简单地写:

class A(object):
    def __init__(foo):
        foo.x = 'Hello'

    def method_a(bar, foo):
        print bar.x + ' ' + foo

结果是一样的。然而,建议使用self,因为其他python更容易识别它。

只是这个问题的一个演示。

class MyClass:

    def __init__(self):
        print('__init__ is the constructor for a class')

    def __del__(self):
        print('__del__ is the destructor for a class')

    def __enter__(self):
        print('__enter__ is for context manager')
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        print('__exit__ is for context manager')

    def greeting(self):
        print('hello python')


if __name__ == '__main__':
    with MyClass() as mycls:
        mycls.greeting()

$ python3 class.objects_instantiation.py
__init__ is the constructor for a class
__enter__ is for context manager
hello python
__exit__ is for context manager
__del__ is the destructor for a class

是的,你是对的,这些是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__(如本文所述,负责初始化新创建的实例)。

一个简单的说明性例子

希望它能有所帮助,这里有一个简单的例子,我用它来理解在类中声明的变量和在__init__函数中声明的变量之间的区别:

class MyClass(object):
    i = 123
    def __init__(self):
        self.i = 345
     
a = MyClass()
print(a.i)
print(MyClass.i)

输出:

345
123

试试这段代码。希望它能帮助很多像我一样的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