学习Python中的self、__init__(self)

时间:2020-09-06 14:31:29   收藏:0   阅读:55

python中的self

class Test:
	def print_self(self):
		print(self)
		print(self.__class__)
		
t = Test()
t.print_self()
<__main__.Test object at 0x102f90ac0>
<class ‘__main__.Test‘>

python中的__init__(self)函数

def __init__(self):
    self.data = []
class Dog:

    kind = ‘canine‘         # class variable shared by all instances

    def __init__(self, name):
        self.name = name    # instance variable unique to each instance

>>> d = Dog(‘Fido‘)
>>> e = Dog(‘Buddy‘)
>>> d.kind                  # shared by all dogs
‘canine‘
>>> e.kind                  # shared by all dogs
‘canine‘
>>> d.name                  # unique to d
‘Fido‘
>>> e.name                  # unique to e
‘Buddy‘

参考文档

原文:https://www.cnblogs.com/kikisara/p/13621222.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!