码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-07-06 00:33:54      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:elf   错误   color   rom   play   姓名   with   运行   NPU   

从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 “类名称的错误”

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

标签:elf   错误   color   rom   play   姓名   with   运行   NPU   

原文地址:https://www.cnblogs.com/pengingintent/p/11141297.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!