Python 2.7 无法从其他文件构造类
以下是我的两个文件: Character.pyPython 2.7 无法从其他文件构造类,python-2.7,canopy,Python 2.7,Canopy,以下是我的两个文件: 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,
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回溯(最近一次呼叫上次)
E:\canopy\canopy\App\appdata\canopy-1.4.0.1938.win-x86\u 64\lib\site packages\IPython\utils\py3compat.pyc在execfile(fname,glob,loc)中
195其他:
196 filename=fname
-->197 exec编译(脚本文本,文件名,'exec'),在glob,loc中
198其他:
199 def execfile(fname,*其中):
()中的C:\Users\Desktop\Helper Functions\New Folder\Interface.py
13来自字符导入*;
14
--->15风=字符(“样本数据”);
16
17
C:\Users\Desktop\Helper Functions\New Folder\Character.py in\uuuuu init\uuuuuu(self,fn)
48对于fp中的行:
49 nline=line.strip().split(“:”);
--->50如果(类型(nline)!=列表):
51继续;
52.其他:
AttributeError:角色实例没有属性“stats”
我想知道这段代码是怎么回事,我导入的方式是否错误?不,您的导入没有问题。您确定两次跑步都在同一位置吗?由于代码只指定文件名,没有路径,因此python会话需要在
Sample.dat
文件所在的目录中运行。之所以问这个问题,是因为您在您的代码的中间定义了一个STATS属性。只有当文件不存在时才会发生这种情况(意味着它看起来不存在,而你正从那里运行)
python中的附言:
- 行尾不需要分号
语句中的条件周围不需要括号if
- 类应派生自
:对象
类字符(对象):
- Docstrings(在方法名称上方加上三个引号的字符串)应该在方法名称的正下方。这将允许ipython和其他工具在用户在其前面打问号时拾取并显示它们
%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'