2014-05-12 89 views
0

这里是我的两个文件:无法从另一个文件构造一类

Character.py

def Check_File(fn): 
    try: 
     fp = open(fn); 
    except: 
     return None; 
    return fp; 


class Character: 

    ## variables ## 
    ## atk ## 
    ## def ## 
    ## HP ## 
    ## empty inv ## 


    ''' 
    init (self, filename), 

    RETURN -1 == if the file is not exist 
    RETURN 0 == all good 

    all files will be save in format of 

    "skill: xxx, xxx; xxx, xxx; xxx, xxx;" 

    ''' 
    def __init__(self, fn): 
     fp = Check_File(fn); 
     if(fp == None): 
      print "Error: no such file" 
      return None; 
     self.stats = {}; 
     for line in fp: 
      nline = line.strip().split(": "); 
      if(type(nline) != list): 
       continue; 
      else: 
       self.stats[nline[0]] = nline[1]; 
       ##print self.stats[nline[0]] 
     fp.close(); 


    ''' 
    display character 
    ''' 
    def Display_Character(self): 

     print "The Character had:"; 
     ## Parse into the character class ## 
     for item in self.stats: 

      print item + ": " + self.stats[item]; 

     print "Finished Stats Displaying"; 


print Character("Sample.dat").stats 

另一条是:

Interface.py

##from Interface_helper import *; 
from Character import *; 

wind = Character("Sample.dat"); 


wind.Display_Character(); 

当我在Character.py中运行代码,它给出了

%run "C:/Users/Desktop/Helper Functions/New Folder/Character.py" 
{'item': 'weird stuff', 'hp': '100', 'name': 'wind', 'def': '10', 'atk': '10'} 

但是当我运行Interface.py:

%run "C:/Users/Desktop/Helper Functions/New Folder/Interface.py" 
--------------------------------------------------------------------------- 
AttributeError       Traceback (most recent call last) 
E:\canopy\Canopy\App\appdata\canopy-1.4.0.1938.win-x86_64\lib\site-packages\IPython\utils\py3compat.pyc in execfile(fname, glob, loc) 
    195    else: 
    196     filename = fname 
--> 197    exec compile(scripttext, filename, 'exec') in glob, loc 
    198  else: 
    199   def execfile(fname, *where): 

C:\Users\Desktop\Helper Functions\New Folder\Interface.py in <module>() 
    13 from Character import *; 
    14 
---> 15 wind = Character("Sample.dat"); 
    16 
    17 

C:\Users\Desktop\Helper Functions\New Folder\Character.py in __init__(self, fn) 
    48   for line in fp: 
    49    nline = line.strip().split(": "); 
---> 50    if(type(nline) != list): 
    51     continue; 
    52    else: 

AttributeError: Character instance has no attribute 'stats' 

我想知道是怎么回事了这一段代码,为什么我输入了错误的方式?

回答

2

不,您的导入没有问题。你确定你在两个跑步中都在同一个位置吗?由于您的代码仅指定文件名而没有路径,因此您的python会话需要在Sample.dat文件所在的目录中运行。我之所以这么问,是因为你在你的__init__的中间定义了一个属性属性,唯一可能发生的不存在的是它上面的return None被调用。只有当文件不存在时才会发生这种情况(这意味着不存在它看起来的位置,这是您运行的位置)。

P.S.在python:

分号不需要在线路
  • 括号不是在if语句所需周围的状况结束
    • 类应该从object得到:class Character(object):
    • 文档字符串(字符串你在方法名称上面加三个引号)应该在方法名称下方。这将允许ipython和其他工具在用户在他们面前放置问号时将其提供并显示出来。
  • +0

    对不起,回复晚了,这是非常有帮助的,我会去仔细检查。谢谢。前两个是我个人对分号和括号的偏好。后两个真的很有帮助。再次感谢。 – windsound

    +0

    我又跑了,它的工作原理很奇怪...... – windsound