当前位置: 移动技术网 > IT编程>脚本编程>Python > day 26-1 property、绑定与非绑定方法

day 26-1 property、绑定与非绑定方法

2019年12月18日  | 移动技术网IT编程  | 我要评论

property

property是一种特殊的属性,访问它时会执行一段功能(函数)然后返回值;就是把一个函数属性的访问方式变成像访问数据属性的方式一样。

我们首先来看一个对比效果

例一:在调用 bmi 函数的时候需要加括号的,可是我们往往需要另一种调用方法——不想加括号

class people():
    def __init__(self, name, height, weight):
        self.name = name
        self.height = height
        self.weight = weight

    def bmi(self):
        return self.weight / (self.height ** 2)


p = people('ysg', 1.8, 75)
print(p.bmi())
# 结果:23.148148148148145

例二:使用 property 后,则调用不需要在使用括号了

class people():
    def __init__(self, name, height, weight):
        self.name = name
        self.height = height
        self.weight = weight

    @property
    def bmi(self):
        return self.weight / (self.height ** 2)


p = people('ysg', 1.8, 75)
print(p.bmi)
# 结果:23.148148148148145
# 使用加括号调用的会报错:typeerror: 'float' object is not callable

 

property 的其他用法,并不常用
前提条件
class people():
    def __init__(self, name):
        self.__name = name

    @property
    def name(self):
        return self.__name


p = people('ysg')
print(p.name)
setter、deleter 的使用
class people():
    def __init__(self, name):
        self.__name = name

    @property
    def name(self):
        return self.__name

    @name.setter
    def name(self, val):
        if not isinstance(val, str):
            print('格式不是 str,不允许被修改')
            return
        self.__name = val
    @name.deleter
    def name(self):
        print('不允许被删除')


修改
p = people('ysg')
p.name = 'ysging'
print(p.name)
结果:ysging

p.name = 213
print(p.name)
结果:
格式不是 str,不允许被修改
ysg

删除
del p.name
print(p.name)
结果:
不允许被删除
ysg
property 意义在于:将一个类的函数定义成特性以后,对象再去使用的时候obj.name,根本无法察觉自己的 name 是执行了一个函数然后计算出来的,这种特性的使用方式遵循了统一访问的原则

绑定方法

在类内部定义的函数,分为两大类

一:绑定方法

  绑定给谁,就应该由谁来调用,谁来调用就会把调用者当作第一个参数自动传入

    绑定对象的方法:在类内定义的没有被任何装饰器修饰的

    绑定到类的方法:在类内定义的被装饰器 classmethod 修饰的方法

二:非绑定方法

  不与类或者对象绑定,没有自动传值
  就是在类中定义一个普通的函数或工具,对象和类都可以调用,都需要传值

 

绑定对象的方法

class foo():
    def __init__(self, name):
        self.name = name

    def tell(self):
        print('this is %s' % self.name)

    @classmethod
    def func(cls):
        print()

f = foo('ysg')
f.tell()            # this is ysg
print(f.tell)       # <bound method foo.tell of <__main__.foo object at 0x000001e5bed2f390>>
print(foo.tell)     # <function foo.tell at 0x000001e5bed31268>
foo.tell(f)         # 使用类来调用  this is ysg

绑定到类的方法

class foo():
    def __init__(self, name):
        self.name = name

    @classmethod
    def func(cls):
        print(cls)



f = foo('ysg')
print(foo.func)         # <bound method foo.func of <class '__main__.foo'>>
foo.func()              # <class '__main__.foo'>
print(foo)              # <class '__main__.foo'>

非绑定方法

class foo():
    def __init__(self, name):
        self.name = name

    @staticmethod
    def func(x, y):
        print(x + y)


f = foo('ysg')
print(foo.func)     # <function foo.func at 0x0000021b45fe1268>
print(f.func)       # <function foo.func at 0x0000021b45fe1268>
f.func(1, 2)        # 3
foo.func(1, 2)      # 3

使用场景

总结:使用哪种方法,取决于函数体的逻辑来进行判断应该传什么参数。

例子:绑定到对象

class people():
    def __init__(self, name, age, sex):
        self.name = name
        self.age = age
        self.sex = sex

    def tell(self):
        print('姓名:{0},年龄:{1},性别:{2}'.format(self.name, self.age, self.sex))


p = people('ysg', 21, 'man')
p.tell()        # 姓名:ysg,年龄:21,性别:man

例子:绑定到类

前提条件,从配置文件中取值

configs.py

name = 'ysging'
age = 15
sex = 'man'
import configs
class people():
    def __init__(self, name, age, sex):
        self.name = name
        self.age = age
        self.sex = sex

    def tell_info(self):
        print('姓名:{0},年龄:{1},性别:{2}'.format(self.name, self.age, self.sex))

    @classmethod
    def tell(cls):
        p = cls(
            configs.name,
            configs.age,
            configs.sex
        )
        return p


p = people.tell()
p.tell_info()       #姓名:ysging,年龄:15,性别:man

例子:非绑定方法,给每一个实例分配 id

import time
import hashlib


class people():
    def __init__(self, name, age, sex):
        self.name = name
        self.age = age
        self.sex = sex
        self.id = self.ids()

    @staticmethod
    def ids():
        time.sleep(0.01)
        m = hashlib.md5(str(time.time()).encode('utf-8'))
        return m.hexdigest()


p = people('ysg1', 21, 'man')
p2 = people('ysg2', 22, 'man')
p3 = people('ysg3', 23, 'man')

print(p.id)   # 44261d399ba9650c628cb8d189ffdd0b
print(p2.id)  # 2c7270c27984119c7cf39e9d575f07bd
print(p3.id)  # 4c386111edf4f641f7c35bb855d1551a

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网