2014-05-12 33 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