为了账号安全,请及时绑定邮箱和手机立即绑定

Python学习:17.Python面向对象(四、属性(特性),成员修饰符,类的特殊成员)

标签:
Python

一、属性(特性)

普通方法去执行的时候,后面需要加括号,特性方法执行的时候和静态字段一样不需要不需要加括号.

特性方法不和字段同名.

特性方法不能传参数.

在我们定义数据库字段类的时候,往往需要对其中的类属性做一些限制,一般用get和set方法来写,那在python中,我们该怎么做能够少写代码,又能优雅的实现想要的限制,减少错误的发生呢,这时候就需要我们的@property.

获取特性

复制代码

class Foo:    def __init__(self,name):
        self.name = name    # 普通方法
    def start(self):
        temp = '%s sb' %self.name        return temp    # 特性,将方法的执行方式变为和字段一样    @property    def end(self):
        temp = '%s gd'%self.name        return temp

obj = Foo('alexsel')
ret1 = obj.start()
ret2 = obj.endprint(ret2)print(obj.name)


输出结果:
alexsel gd
alexsel

复制代码

设置特性

设置特性方法的时候,所需要加的装饰器名字规则是,你所设置特性方法名字点setter(例如:@end.setter)

复制代码

class Foo:    def __init__(self,name):
        self.name = name    # 普通方法
    def start(self):
        temp = '%s sb' %self.name        return temp    # 特性,将方法的执行方式变为和字段一样    @property    def end(self):
        temp = '%s gd'%self.name        return temp    # 如果需要使用设置特性的的方法,就需要这个点前面名字和所要设置特性的方法同名,就像这里的end    @end.setter    def end(self,value):        print(value)
        self.name = value

obj = Foo('alexsel')#获取特性,获取特性的时候,拿到的是@Property的返回值ret2 = obj.endprint(ret2)#设置特性,设置特性的时候,会执行@end.setter下面的方法,这个'aaa'就传递给valueobj.end = 'aaa'ret1 = obj.endprint(ret1)


输出结果:
alexsel gd
aaa
aaa gd

复制代码

这个特性在python中不是特别常用,因为我们一般可以使用普通的方法代替特性方法。

 

二、成员修饰符

 首先介绍‘__’,这个在命名之前添加就会变成私有的,只有在类的内部才能访问。

复制代码

class Foo:

    book = 'alexsel'
    #命名的时候前面添加__,只有在类的内部才能访问,在外部无法访问
    __book = 'book'

    def __init__(self):
        self.__name = 'alexsel'


    def start(self):        print(Foo.__book)        print(self.__name)    def __end(self):        print('__end')


obj = Foo()print(obj.book)# print(obj.__book) #这种无法拿到字段#在外部也无法调用类私有方法#obj.__end()#通过内部方法调用拿到字段obj.start()


输出结果:
alexsel
book
alexsel

复制代码

私有的属性只有自己可以访问,当某个类继承这个类之后,也无法访问父类的私有属性。

复制代码

class Foo:

    book = 'alexsel'
    __book = 'book'

    def __init__(self):
        self.__name = 'alexsel'


    def start(self):        print(Foo.__book)        print(self.__name)
        self.__end()    def __end(self):        print('__end')class Bar(Foo):    def start(self):        print(self.__name)#子类继承父类,也无法调用父类的私有属性obj = Bar()
obj.start()

输出结果:
报错

复制代码

静态方法也是如此

复制代码

class Foo:

    book = 'alexsel'
    __book = 'book'

    def __init__(self):
        self.__name = 'alexsel'


    def start(self):
        Foo.__add() #内部调用私有静态方法


    def __end(self):        print('__end')


    @staticmethod    def __add():        print('add')


obj = Foo()
obj.start()


输出结果:
add

复制代码

python成员修饰符有两种,一种是共有的,一种是私有的,共有的都可以访问,私有的只有自己可以访问,或者在外部间接访问。

但是还有一种强行访问私有属性的方法,如下:

复制代码

class Foo:

    book = 'alexsel'
    __book = 'book'

    def __init__(self):
        self.__name = 'alexsel'


    def start(self):
        Foo.__add() #内部调用私有静态方法


    def __end(self):        print('__end')


    @staticmethod    def __add():        print('add')


obj = Foo()print(obj._Foo__book)


输出结果:
book

复制代码

虽然可以使用,但是不推荐这种使用方法。

三、类的特殊成员

1.call

__call__()的作用是使实例能够像函数一样被调用,同时不影响实例本身的生命周期(__call__()不影响一个实例的构造和析构)。

 __call__ 方法的执行是由对象后加括号触发的,即:对象() 或者 类()()。

复制代码

class Foo:    def __init__(self):        print( Foo:      def 'init')    def __call__(self, *args, **kwargs):        print('call')        return 1r = Foo()
r()
r = Foo()()#加第一个括号执行__init__,执行完__init__,获取到一个对象,对象加一个括号就是执行__call__,拿到返回值print(r)


输出结果:
init
call
init
call1

复制代码

类后面添加括号执行__init__方法,对象后面加括号执行__call__方法。

2.getitem,setitem,delitem

用于索引操作,如字典。以上分别表示获取、设置、删除数据。

首先得实例是字典类型的操作。

复制代码

class Foo:    def __init__(self):        print('init')    def __call__(self, *args, **kwargs):        print('call')        return 1    def __getitem__(self, item):        print(item)    def __setitem__(self, key, value):        print(key,value)    def __delitem__(self, key):        print(key)

r = Foo()
r() #调用__call__方法r['k1']#使用中括号传参数的时候,默认使用过的是__getitem__方法r['xxx'] = 123#这里的xxx传给__setiem__的key,123传给__setitem__的value。del r['xxx']    #删除的时候,调用的是__delitem__方法输出结果:
init
call
k1

复制代码

接下来是切片类型的操作,在切片操作的时候,在2.x版本中,执行的是__getslice_,__setslice__,delslice__方法,在3.x中的版本执行的是还是__getitem__,__setitem__,__delitem__方法。

复制代码

class Foo:    def __init__(self):        print('init')    def __call__(self, *args, **kwargs):        print('call')        return 1    def __getitem__(self, item):        print(item,type(item),'__getitem__')    def __setitem__(self, key, value):        print(key,value)    def __delitem__(self, key):        print(key)

r = Foo()#使用切片的时候,在2.x版本中,调用的是__getslice__方法,在3.x中调用的是__getitem__方法。r[1:3]
r[1:3] = [11,22,33] #这里执行__setitem__方法del r[1:3]  #在这里执行__delitem__方法

复制代码

 3.dict

dict是用来存储对象属性的一个字典,其键为属性名,值为属性的值。

复制代码

class Foo:    """
    我是一个注释    """

    book = 'alexsel'
    __book = 'book'

    def __init__(self):
        self.__name = 'alexsel'


    def start(self):
        Foo.__add() #内部调用私有静态方法


    def __end(self):        print('__end')


    @staticmethod    def __add():        print('add')


obj = Foo()print(obj.__dict__) #获取对象里面所有字段print(Foo.__dict__)

输出结果:
{'_Foo__name': 'alexsel'}
{'book': 'alexsel', '_Foo__book': 'book', '__init__': <function Foo.__init__ at 0x00000000027CF950>, '__dict__': <attribute '__dict__' of 'Foo' objects>, 'start': <function Foo.start at 0x00000000027EBB70>, '_Foo__add': <staticmethod object at 0x00000000027B6390>, '__weakref__': <attribute '__weakref__' of 'Foo' objects>, '_Foo__end': <function Foo.__end at 0x00000000027EBBF8>, '__doc__': '\n    我是一个注释\n    ', '__module__': '__main__'}

复制代码

4.__iter__

类的迭代器可以使用for循环迭代类。

如果创建的对象可以被迭代,在类的内部就执行了__iter__方法。

复制代码

class Foo:    def __iter__(self):        yield 1        yield 2        yield 3        yield 4obj = Foo()#如果执行for对象时,自动会执行对象的iter方法for i in obj:    print(i)



输出结果:1
2
3
4

复制代码

原文出处:https://www.cnblogs.com/liudi2017/p/9383838.html

点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消