python class instance_例析python的class和instance

print

p.__dict__

print

p.__class__

result:------

1

2

{'__module__': '__main__', '__doc__': None}

{'y': 2, 'x': 1}

__main__.point

------

comments:

1.this shows how class and instance be created

2.note that class might have no attribute at first,

we can add attribute latter.

3.__dict__ is the inherent attribute of class and instance,

all attributes of class and instance are stored in __dict__

4.__class__ is the inherent attribute of an instance,

the class name of the instance is stored in it

example code2:

class

point

:

def

__init__

(

self

):

self

.x=

1

self

.y=

2

p=point()

print

p.x

print

p.y

print

point.__dict__

print

p.__dict__

print

p.__class__

print

dir(p)

result:------

1

2

{'__module__': '__main__', '__doc__': None,

'__init__': 

__init__ at

0x022BE2F0>}

{'y': 2, 'x': 1}

__main__.point

['__doc__', '__init__', '__module__', 'x', 'y']

------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值