免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 12604 | 回复: 0

python初学者误区-模块名与同名类的使用 [复制链接]

论坛徽章:
0
发表于 2010-01-14 21:59 |显示全部楼层
Python的模块名与类名是在两个不同的名字空间中,初学者很容易将其弄混淆。
看下面的例子:
这个例子想通过继承类ConfigParser来实现对类似windows的ini文件的操作,
修改了ConfigParser的将配置选项名全部转为小写的操作,使得配置选项名支持
大小写混用。
========================
#!/usr/bin/env python
# -*- coding: cp936 -*-
import ConfigParser
class CAppConfig(ConfigParser):
    ''' 提供配置文件的操作,增加以下功能:
    1. 大写的配置项和小写的配置项不同
    '''
    def setValue(self,section,option,value):
        ''' 修改配置项的值: [section].option=value '''        
        self.set(section,option,value)
    def getValue(self,section,option):
        ''' 获取配置项的值: [section].option '''
        try:
            value = self.get(section,option)
        except ConfigParser.NoOptionError:
            value = ""
        return value
    def optionxform(self,optionstr):
        ''' 防止大小写转换'''
        return optionstr            
    def __init__(self,filename):
        ConfigParser.__init__(self)
        self.filename=filename
        self.read(filename)
    def __del__(self):
        fp = open(self.filename,"w")
        self.write(fp)
        fp.close()
   
if __name__ == "__main__":
    config = CAppConfig("test.ini")
    print config.sections()
    print config.options("SERVER")
   
    print config.getValue("SERVER","TEST")
    print config.getValue("SERVER","test")
    config.setValue("SERVER","TEST","test")
    print config.getValue("SERVER","TEST")
    print config.getValue("SERVER","notexist")
编译这段python脚本,会提示如下错误:
    class CAppConfig(ConfigParser):
TypeError: Error when calling the metaclass bases
    module.__init__() takes at most 2 arguments (3 given)
原因在于,import 使用的名字ConfigParser 是包名,而在类定义CAppConfig(ConfigParser)中使用是类名。因此,需要将ConfigParser换成类名:ConfigParser.ConfigParser,
修改后,发现还是有问题:
    ConfigParser.__init__(self)
TypeError: module.__init__() argument 1 must be string, not instance
Exception AttributeError: "CAppConfig instance has no attribute 'filename'" in >
ignored
这个错误的原因在于, 类的初始化函数中ConfigParser.__init__(self),使用的还是类名。
那既然ConfigParser和ConfigParser都是类名,可以直接将 import ConfigParser修改为:
from ConfigParser import ConfigParser即可,可是这样修改后发现还有问题:
    except ConfigParser.NoOptionError:
AttributeError: class ConfigParser has no attribute 'NoOptionError'
这个错误原因在于ConfigParser使用的还是包名,在出现异常的时候,找不到包中定义的NoOptionError类。
经过这几部实验,发现ConfigParser 和ConfigParser使用的是包名,ConfigParser和ConfigParser使用的是类名。因此在最终版本中,将import ConfigParser 和ConfigParser.__init__不变, 将ConfigParser和ConfigParser修改该成为ConfigParser.ConfigParser就可以了。
======== 附件是修改后的正确代码,和测试数据。

       
        文件:app_config.tar
        大小:4KB
        下载:
下载
       
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/110228/showart_2149365.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP