将函数修饰符向后移植到 python2.1
我有一些用 python 2.4++ 开发的代码,你敢打赌,我必须将其向后移植到 python 2.1!
函数装饰器非常有吸引力,以至于我在一些地方使用了 @classmethod,但没有注意到它仅从 2.4 版本开始可用。 函数修饰符提供了相同的功能,但它们出现在 python 2.2 中。 CFR: http://www.python.org/dev/peps/pep-0318 /
现在我们的一些客户似乎仍然依赖于 python 2.1(ArcGIS 9.1 附带它并且使其不可升级),甚至连功能修饰符都不可用...
我一直在寻找一些功能修饰符python 2.1 中的定义,但我没有找到任何(我的意思是:工作)。 有人成功解决这个问题吗?
更具体地说,我需要一种在 python 2.1 中运行此 2.4 代码的方法:
Python 2.4.6 (#2, Mar 19 2009, 10:00:53)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class sic:
... def f(cls):
... print cls.__name__
... f = classmethod(f)
...
>>> sic().f()
sic
>>> sic.f()
sic
>>>
I have some code I developed in python 2.4++ and you bet, I have to backport it to python 2.1!
function decorators were so attractive that I have used @classmethod at a few spots, without taking notice of the fact that it is only available starting at version 2.4. the same functionality is offered by function modifiers but these appeared in python 2.2. cfr: http://www.python.org/dev/peps/pep-0318/
now some of our customers appear to be still dependent on python 2.1 (ArcGIS 9.1 ships with it and makes it not upgradable), where not even the function modifiers are available...
I have been looking for some function modifier definitions in python 2.1, but I did not find any (I mean: working). anybody successfully solved this problem?
to be more concrete, I need a way to run this 2.4 code in python 2.1:
Python 2.4.6 (#2, Mar 19 2009, 10:00:53)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class sic:
... def f(cls):
... print cls.__name__
... f = classmethod(f)
...
>>> sic().f()
sic
>>> sic.f()
sic
>>>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果只是 @classmethod 需要向后移植到 Python 2.1。
有一个python2.1中Classmethod模拟的配方
希望这有帮助。
If just the @classmethod need to be backported to Python 2.1.
There is a recipe of Classmethod emulation in python2.1
Hope this help.
就像我在 2.1 静态方法的旧配方中一样:
您应该能够执行 2.1 类方法:
当然没有
@
-syntax,而是旧方法(如 2.2 中):如果这不'不起作用(抱歉,自从我有一个 Python 2.1 可供测试以来已经很多年了),需要更明确地提供该类 - 并且由于您在类对象存在之前调用 classmethod,因此需要按名称命名 - - 假设一个全局类,
很难找到优雅的方法来获取类对象(抑制对 self 的需求是容易的部分,而静态方法就足够了:您只需将函数包装成非函数可调用),因为2.1 只有遗留(旧式类),没有可用的元类,因此没有真正好的方法让 sic2.f() 神奇地获得 cls。
由于 2.1 中缺少 @ 语法不可避免地需要编辑使用
@classmethod
的代码,因此另一种方法是将功能(将某些方法装饰为“类”方法)移至class
语句(好处是当时类对象确实存在)。Just like in my old recipe for 2.1 staticmethod:
you should be able to do a 2.1 classmethod as:
No
@
-syntax of course, but rather the old way (like in 2.2):If this doesn't work (sorry, been many many years since I had a Python 2.1 around to test), the class will need to be supplied more explicitly -- and since you call classmethod before the class object exists, it will need to be by name -- assuming a global class,
It's really hard to find elegant ways to get the class object (suppressing the need for self is the easy part, and what suffices for staticmethod: you just need to wrap the function into a non-function callable) since 2.1 had only legacy (old-style classes), no usable metaclasses, and thus no really good way to have sic2.f() magically get that cls.
Since the lack of @ syntax in 2.1 inevitably requires editing of code that uses
@classmethod
, an alternative is to move the functionality (decorating some methods to be "class" ones) to right AFTER the end of theclass
statement (the advantage is that the class object does exist at that time).