Skip to main content

Command Palette

Search for a command to run...

Python 类的高级函数(双下横线)

Published
2 min read
Python 类的高级函数(双下横线)

类的高级函数(双下横线)

__str__

介绍
  • 如果定义了该函数, 当print当前实例化对象的时候, 会返回改函数的return信息
用法
  def __str__(self):
      return str_type
参数
返回值
  • 一般返回对于该类的描述信息

__gatattr__

介绍
  • 当调用的属性或方法不存在时,会返回该方法定义的信息
用法
def __gatattr__(self, key):
    print(something...)
参数
  • key : 调用任意不存在的属性名
返回值
  • 可以是任意类型也可以不进行返回

代码片段1

# coding:utf-8

class Test(object):

    def __str__(self):
        return 'this is a test class'

    def __getattr__(self, key):
        # print('这个key:{}并不存在'.format(key))
        return '这个key:{}并不存在'.format(key)


t = Test()
print(t)
# print(t.a)
print(t.a)
print(t.b)

__setattr__

功能
  • 拦截当前类中不存在的属性与值
用法
def __settattr__(self, key, value):
    self.__dict__[key] = value
参数
  • key 当前的属性名
  • value 当前的参数对应的值
返回值

__call__

功能
  • 本质是将一个类变成一个函数
用法
def __call__(self, *args, **kwargs):
    print('call will start')
参数
  • 可传任意参数
返回值
  • 与函数情况相同 可有可无

代码片段2


# coding:utf-8

class Test(object):

    def __str__(self):
        return 'this is a test class'

    def __getattr__(self, key):
        # print('这个key:{}并不存在'.format(key))
        return '这个key:{}并不存在'.format(key)

    def __setattr__(self, key, value):
        # print(key, value)
        # if key not in self.__dict__:
        self.__dict__[key] = value
        print(self.__dict__)

    def __call__(self, a):
        print('call func will start')
        print(a)


t = Test()
print(t)
# print(t.a)
print(t.a)
print(t.b)
t.name = '小慕'
print(t.name)
t('dewei')
# t.a.b.c 链式操作


class Test2(object):
    def __init__(self, attr=''):
        self.__attr = attr

    def __call__(self, name):
        # print('key is {}'.format(self.__attr))
        return name

    def __getattr__(self, key):
        if self.__attr:
            key = '{}.{}'.format(self.__attr, key)
        else:
            key = key
        print(key)
        return Test2(key)


t2 = Test2()
name = t2.a.b.c('dewei')
print(name)


result = t2.name.age.sex('ok')
print(result)
7 views

More from this blog

MySQL | 表的内连接

数据操作语言:表连接查询(一) 从多张表中提取数据 从多张表提取数据,必须指定关联的条件。如果不定义关联条件就会出现无条件连接,两张表的数据会交叉连接,产生 笛卡尔积。 规定了连接条件的表连接语句,就不会出现笛卡尔积。 # 查询每名员工的部门信息 SELECT e.empno,e.ename,d.dname FROM t_emp e JOIN t_dept d ON e.deptno=d.deptno; 表连接的分类 表连接分为两种:内连接 和 外连接 内连接是结果集中只保留符合...

May 16, 20221 min read13
MySQL | 表的内连接

MySQL | 分组查询的应用

数据操作语言:分组查询 为什么要分组? 默认情况下汇总函数是对全表范围内的数据做统计 GROUP BY 子句的作用是通过一定的规则将一个数据集划分成若干个小的区域,然后针对每个小区域分别进行数据汇总处理 SELECT deptno,AVG(sal) FROM t_emp GROUP BY deptno; SELECT deptno,ROUND(AVG(sal)) FROM t_emp GROUP BY deptno; -- ROUND 取整 逐级分组 数据库支持多列分组条件,执行的时候...

Apr 27, 20221 min read10
MySQL | 分组查询的应用

MySQL | 聚合函数的使用

数据操作语言:聚合函数 什么是聚合函数 聚合函数在数据的查询分析中,应用十分广泛。聚合函数可以对 数据求和、求 最大值 和 最小值 、求 平均值 等等。 求公司员工的评价月收入是多少? SELECT AVG(sal+IFNULL(comm,0)) FROM t_emp; SELECT AVG(sal+IFNULL(comm,0)) AS avg FROM t_emp; SUM 函数 SUM 函数用于求和,只能用户数字类型,字符类型的统计结果为 0 ,日期类型统计结果是毫秒数相加 SE...

Apr 26, 20221 min read8
MySQL | 聚合函数的使用
U

Untitled Publication

173 posts