面向对象时遇见的属性错误(AttributeError)

从teacher.py文件中运行是正常运行的

当调用到bin文件夹下的start.py文件下运行时出现属性错误

错误的源码:

# teacher.py
# _*_coding:utf-8_*_
import os
import pickle


class Path:
    teacher_path = "%s\\log" % os.path.dirname(os.getcwd())


class Teacher:
    def __init__(self, name, age, phone):
        self.name = name
        self.age = age
        self.phone = phone

    def look_teacher(self):
        teacher_list = """姓名:%s\n年龄:%s\n手机号:%s
        """ % (self.name, self.age, self.phone)
        print(teacher_list)

    def write_text(self):
        teacher_path = "%s\\%s" % (Path.teacher_path, self.name)
        with open(teacher_path, "wb") as f:
            pickle.dump(self, f)


def read_file(word_one):
    student_bool = False
    teacher_bool = False
    while True:
        for i_text_one in os.listdir(Path.teacher_path):
            text_path = "%s\\%s" % (Path.teacher_path, i_text_one)
            with open(text_path, "rb") as f:
                text_word = pickle.load(f)
            if text_word.phone == word_one:
                st_obj = text_word
                teacher_bool = True
        if student_bool or teacher_bool:
            print("%s已创建过" % st_obj.name)
            no_write_bool = True
        else:
            no_write_bool = False
        break
    return no_write_bool


def run_teacher():
    while True:
        name = input("name:")
        age = int(input("age:"))
        phone = int(input("phone:"))
        t_bool = read_file(phone)
        if not t_bool:
            Teacher(name, age, phone).write_text()
            print("\033[1;34m%s创建成功\033[0m" % name)
        else:
            print("\033[1;34m%s已创建\033[0m" % name)


if __name__ == '__main__':
    run_teacher()



# start.py

# _*_coding:utf-8_*_
from core import teacher

if __name__ == '__main__':
    teacher.run_teacher()
View Code

改正后的源码:

# teacher.py

# _*_coding:utf-8_*_
import os
import pickle


class Path:
    teacher_path = "%s\\log" % os.path.dirname(os.getcwd())


class Teacher:
    def __init__(self, name, age, phone):
        self.name = name
        self.age = age
        self.phone = phone

    def look_teacher(self):
        teacher_list = """姓名:%s\n年龄:%s\n手机号:%s
        """ % (self.name, self.age, self.phone)
        print(teacher_list)

    def write_text(self):
        teacher_path = "%s\\%s" % (Path.teacher_path, self.name)
        with open(teacher_path, "wb") as f:
            pickle.dump(self, f)


def read_file(word_one):
    student_bool = False
    teacher_bool = False
    while True:
        for i_text_one in os.listdir(Path.teacher_path):
            text_path = "%s\\%s" % (Path.teacher_path, i_text_one)
            with open(text_path, "rb") as f:
                text_word = pickle.load(f)
            if text_word.phone == word_one:
                st_obj = text_word
                teacher_bool = True
        if student_bool or teacher_bool:
            print("%s已创建过" % st_obj.name)
            no_write_bool = True
        else:
            no_write_bool = False
        break
    return no_write_bool


def run_teacher():
    while True:
        name = input("name:")
        age = int(input("age:"))
        phone = int(input("phone:"))
        t_bool = read_file(phone)
        if not t_bool:
            Teacher(name, age, phone).write_text()
            print("\033[1;34m%s创建成功\033[0m" % name)
        else:
            print("\033[1;34m%s已创建\033[0m" % name)


if __name__ == '__main__':
    run_teacher()



# start.py

# _*_coding:utf-8_*_
from core.teacher import Teacher
from core import teacher

if __name__ == '__main__':
    teacher.run_teacher()
View Code

总结:当在写入文件内用到新建类的属性时,需要在运行代码.py文件内调用类的名称,不管是否用这个新建类,否则在程序运行中会出现AttributeError: Can't get attribute “类名称的错误”

猜你喜欢

转载自www.cnblogs.com/pengingintent/p/11141297.html