首页 > 编程语言 > 详细

python 自定义类写法

时间:2020-11-24 12:28:45      阅读:45      评论:0      收藏:0      [点我收藏+]
# 模板1
class LayerChart(TopLevelMixin, _EncodingMixin, core.TopLevelLayerSpec):
    def __init__(self, data=Undefined, layer=(), **kwargs):
        for spec in layer:
            _check_if_valid_subspec(spec, "LayerChart")
            _check_if_can_be_layered(spec)
        super(LayerChart, self).__init__(data=data, layer=list(layer), **kwargs)
        self.data, self.layer = _combine_subchart_data(self.data, self.layer)


# 模板2
#1. 定义
from log import logger
class AsstException(Exception):
    def __init__(self, message):
        super().__init__(message)
        logger.error(message)


#2. 使用
from exception import AsstException
class Messenger(object):
    def __init__(self, sc_key):
        if not sc_key:
            raise AsstException(‘sc_key can not be empty‘)



#模板3
# 装饰类中函数的装饰器第一个参数是self
def connection_closed_handler(function):
    @wraps(function)
    def _wrap(inst, *args, **kwargs):
        try:
            return function(inst, *args, **kwargs)

        except (ConnectionClosedException, WebSocketConnectionClosedException):
            inst.logger.warn("the connection lost. Resetting ...")
            inst.reset(args[1])
            return function(inst, *args, **kwargs)
    return _wrap


#模板4
class A:
     def add(self, x):
         y = x+1
         print(y)
class B(A):
    def add(self, x):
        super().add(x)
b = B()
b.add(2)  # 3

python 自定义类写法

原文:https://www.cnblogs.com/amize/p/14029352.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!