折腾:
期间,
想要实现:
类似于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中给已有的类库增加新功能