python 自动加载,在Python中自动加载

In the past I've used perl's AUTOLOAD facility for implementing lazy loading of symbols into a namespace, and wanted the same functionality in python.

Traditionally the closest you appear to be able to get is to use a class and a __getattr__ class to achieve this sort of thing. However I've also tried rummaging around in sys.modules, and come up with this:

# mymod.py

def greet(greeting="Hello World"):

print greeting

class autoload(object):

def __init__(self, __name__):

super(autoload, self).__init__()

self.wrapped_name = __name__

self.wrapped = sys.modules[__name__]

def __getattr__(self, name):

try:

return getattr(self.wrapped, name)

except AttributeError:

def f():

greet(name+" "+self.wrapped_name)

return f

if __name__ != "__main__":

import sys

sys.modules[__name__] = autoload(__name__)

This does work the way I'd like from a user perspective:

~> python

Python 2.5.1 (r251:54863, Jan 10 2008, 18:01:57)

[GCC 4.2.1 (SUSE Linux)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> import mymod

>>> mymod.hello()

hello

>>> from mymod import Hello_World

>>> Hello_World()

Hello_World

But it strikes me - is there a standard approach that people tend to use for autoloading in python?

Secondly, a question for experienced python developers really is "does this strike you as good or bad practice"? I'm a reasonably experienced python developer, and it strikes me as really useful, but it strikes me as borderline and interested in whether this can be viewed as good practice, bad practice or similar.

Thanks!

解决方案

To answer the question of using a class to impersonate a module:

Yes, the functionality is not accidental. It has been there since early in the 2.x series and still works in the 3.x series.

To answer the question of lazy loading:

There are several ways to do it, and each one is going to be a bit mysterious. Using a module impersonator is a fine method.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值