避免模块名称损坏的干净方法? [英] Clean way to circumvent name mangling for a module?

查看:33
本文介绍了避免模块名称损坏的干净方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python的ctype来连接一个C库文件,运行ctypesgen来创建我可以导入的包装器模块(例如api.py)。碰巧的是,许多C函数和宏的名称都以双下划线开头,当在类定义中使用时,它们被Python视为"私有",并调用名称损坏。

例如,这是可行的:

import api
api.__someGetter()

但这不是

import api
class MyClass(object):
    i = api.__someGetter()

AttributeError: 'module' object has no attribute '_MyClass__someGetter'

起作用

i = api.__dict__['__someGetter']()

但对我来说,它似乎不是很优雅,也不容易阅读--代码中将充斥着对字典键的调用。

有没有更好的方法来禁用或解决模块的名称损坏?

(它是一个C库这一事实可能与答案无关,但希望它解释了为什么我不能简单地更改命名约定。)

推荐答案

出于好奇,这里有一个实现此效果的方法。它将模块包装在一个具有custom attribute access methods的类中,这个类只丢弃了损坏名称的第一部分。这当然是一种黑客行为,并不是在所有情况下都能起作用--我不推荐认真使用它!

    class Wrapper(object):
        def __init__(self, module):
            for name in module.__dict__:
                self.__dict__[name] = module.__dict__[name]

        #__getattr__ is called only if regular access failed
        def __getattr__(self, name): 
            try:
                return self.__dict__['__'+name.split('__', 1)[1]]
            except (IndexError, KeyError):
                raise AttributeError, name

        def __setattr__(self, name, value):
            try:
                self.__dict__['__'+name.split('__', 1)[1]] = value
            except IndexError:   #no double underscores to split
                self.__dict__[name] = value

用法:

import cwrapper 
api = Wrapper(cwrapper)

class MyClass(object):
    i = api.__some_Getter() #works

这篇关于避免模块名称损坏的干净方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆