博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一个类简单说明Python开发命名规范
阅读量:4198 次
发布时间:2019-05-26

本文共 940 字,大约阅读时间需要 3 分钟。

最近一直在撸Python…小结一下

GLOBAL_VAR = ('GLOBAL_VAR')  # 全局常量,大写字母,下划线分割# 类名,以驼峰式命名,要求以object作为继承类# 继承自 object 是为了使属性(properties)正常工作# 使其不受Python 3000的一个特殊的潜在不兼容性影响. 同时也实现了object的默认语义class BaseClass(object):    class_var = 'class_var'  # 类变量    def __init__(self):  # 构造函数        self.inst_var = 'inst_var'  # 成员变量        self._mod_or_protected_var = '_mod_or_protected_var'  # 模块变量或者protected变量,以单个下划线开头        self.__private__var = '__private__var'   # 私有变量,以双下划线开头    def inst_method(self, param):  # public实例函数,以self开头变量的函数        print(self.__private__var)        print(param)    def _mod_protected_method(self):  # 模块或者protected方法,以单个下划线开头,其他模块import * from时候不会导入        pass    def __private_method(self):  # 私有方法,以双下划线开头        pass    @classmethod    def class_method(cls, var):  # 类方法,第一个参数是cls,用classmethod修饰        print(var)    @staticmethod    def static_method(param):  # 静态方法,没有self或者cls作为第一个参数,同时以staticmethod修饰        print(param)

转载地址:http://ugwli.baihongyu.com/

你可能感兴趣的文章
设计模式--原型模式(ProtoType)
查看>>
设计模式--适配器(Adapter)
查看>>
设计模式--桥接模式(Bridge)
查看>>
设计模式--组合模式(composite)
查看>>
设计模式--装饰模式(Decorator)
查看>>
设计模式--外观模式(Facade)
查看>>
设计模式--享元模式(Flyweight)
查看>>
设计模式--代理模式(Proxy)
查看>>
设计模式--责任链模式(COR)
查看>>
设计模式--命令模式(Command)
查看>>
设计模式--解释器模式(Interpreter)
查看>>
设计模式--迭代器模式(Iterator)
查看>>
设计模式--中介者模式(Mediator)
查看>>
设计模式--备忘录模式(Memento)
查看>>
设计模式--观察者模式(Observer)
查看>>
设计模式--状态模式(State)
查看>>
设计模式--策略模式(Strategy)
查看>>
设计模式--模版方法(TemplateMethod)
查看>>
设计模式--访问者模式(Visitor)
查看>>
软件需求分析--三步走
查看>>