Question

Here are my two files:

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

Another one is:

Interface.py

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

wind = Character("Sample.dat");


wind.Display_Character();

When I run the code in Character.py, it gives

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

But when I run Interface.py:

I had

%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' 

I was wondering what is going on for this piece of code, did I import the wrong way?

Was it helpful?

Solution

No there is no issue with your import. Are you sure that you are in the same location for both runs? Since your code just specifies the filename with no path, your python session needs to run in the directory where the Sample.dat file is located. The reason why I am asking this, is because you define a stats attribute in the middle of your __init__ and the only thing that can happen for this not to exist is for the return None above it to be invoked. Which happens only when the file doesn't exist (meaning doesn't exist where it looks, which is where you are running from).

P.S. in python:

  • Semicolons are not needed at the end of lines
  • Parentheses are not needed around the condition in an if statement
  • Classes should derive from object: class Character(object):
  • Docstrings (the strings you put in triple quotes above the method names) should be right below the method name. That will allow ipython and other tools to pick them up and display them when users put a question mark in front of them.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top