最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[未解决]Python中给已有的类库增加新功能

Python crifan 2091浏览 0评论

折腾:

[记录]Flask中给活动到期添加提醒功能

期间,

想要实现:

类似于swift中extension的功能

对于已有的类库,增加新功能

类似于这样的:

把:

# convert local GMT8 to GMT time
# note: input should be ‘datetime’ type, not ‘time’ type
def convertLocalToGmt(localTime) :
    return localTime – timedelta(hours=8);

改造为:

Class Datetime()
    def convertLocalToGmt(self) :
        return localTime – timedelta(hours=8)

然后别的,只要是datetime的话,可以直接使用:

someDattimeValue.convertLocalToGmt()

即可。

python extend existing class

How to extend a class in python? – Stack Overflow

python – Adding a Method to an Existing Object Instance – Stack Overflow

python – How can I extend a class instance? – Stack Overflow

How to Extend Classes to Make New Classes in Python

class – What is the difference between old style and new style classes in Python? – Stack Overflow

Improve Your Python: Metaclasses and Dynamic Classes With Type

An Introduction to Classes and Inheritance (in Python) – Jessica Hamrick

9. Classes — Python 2.7.12 documentation

-》好像无法实现,类似于swift中的extension

只能实现类的继承,弄出一个新的类

有机会再去深究。

转载请注明:在路上 » [未解决]Python中给已有的类库增加新功能

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.180 seconds, using 22.21MB memory