python学习日记(7)

类的私有变量

定义

#class的定义
class example:
    def __init__(self, name):
        example.name = name

#或者
class example(object):
    def __init__(self, name):
        example.name = name

继承

class son(father):
    def __init__(self, name):
        example.name = name

私有变量

class example:
    def __init__(self, name):
        example.__name = name
#__name就是私有

鸭子类型

python的继承是多态的,但是却不是严格多态的

class father(object):
    def run(self):
        print("father")

class son(father):
    pass

class other(object):
    def run(self):
        print("other")

def run2(father):
    father.run()
    father.run()
  • run2虽然是传入father的,但是传入other也无所谓。所谓other看起来像father,所以它就可以看作是father拿来用

run2(father())
run2(son())
run2(other())
#皆可

判断数据类型

type

type("abc") == str

isinstance()

isinstance("abc", str)

dir()

#一次性获得所有属性和方法
dir(father())
#或者
dir(father)

getattr()、setattr()以及hasattr()

获得,设置,是否存在某个属性attr

hasattr(father, "run")
setattr(father, "__document__", "test")
getattr(father, "__document__")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值