You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python类方法列出模拟大学教职工信息时遇属性报错求助

问题:Python类方法调用报错 'str' object has no attribute 'dean_print'

我想用Python类方法列出模拟大学教职工的姓名与职位,运行代码时出现错误:'str' object has no attribute 'dean_print',无法输出预期的各职位人员列表。相关代码如下:

class TSC:

    def __init__(self, President, Dean1, Dean2, Dean3, Dean4, Dean5, Chair1, Chair2, Chair3, Chair4, Chair5, Chair6, Chair7, Chair8, Chair9, Chair10):
        self.president = President
        self.dean_one = Dean1
        self.dean_two = Dean2
        self.dean_three = Dean3
        self.dean_four = Dean4
        self.dean_five = Dean5
        self.chair_one = Chair1
        self.chair_two = Chair2
        self.chair_three = Chair3
        self.chair_four = Chair4
        self.chair_five = Chair5
        self.chair_six = Chair6
        self.chair_seven = Chair7
        self.chair_eight = Chair8
        self.chair_nine = Chair9
        self.chair_ten = Chair10

    def pres(self):
        print(f"The President of TSC is ", end="")
        print(self.president)
   
    def deans(self):
        print(f"The dean of the school of Arts & Sciences is ", end = "")
        self.dean_one.dean_print()
       
        print(f"The dean of the school of Education is ", end = "")
        print(self.dean_two.dean_print1())
       
        print(f"The dean of the school of Nursing and Wellness is ", end = "")
        print(self.dean_three.dean_print1())
       
        print(f"The dean of the school of Buisness ans Digital Media is ", end = "")
        print(self.dean_four.dean_print1())
       
        print(f"The dean of the school of Wizrady is ", end = "")
        print(self.dean_five.dean_print1())


class Arts_and_Sciences:

    def __init__(self, dean):
        self.dean_one = dean
    def dean_print(self):
        print(f"{self.dean_one}")
   
    def heads_AnS(self):
        print(f"The head of of Art is ", end = "")
        print(self.chair_one.heads1_print())
       
        print(f"The head of Science is ", end = "")
        print(self.chair_two.heads2_print())
   


class Education:

    def __init__(self, dean):
        self.dean_two_b = dean
    def dean_print1(self):
        print(f"{self.dean_two_b}")
    def heads_Edu(self):
        print(f"The head of Education is ", end = "")
        print(self.chair_three.heads3_print())
       
        print(f"The head of Teaching Skills is ", end = "")
        print(self.chair_four.heads4_print())


class Nursing:

    def __init__(self, dean):
        self.dean_one_b = dean
    def dean_print1(self):
        print(f"{self.dean_three_b}")
    def heads_Nrs(self):
        print(f"The head of Nursing is ", end = "")
        print(self.chair_five.heads5_print())
       
        print(f"The head of Wellness is ", end = "")
        print(self.chair_six.heads6_print())

class Business:

    def __init__(self, dean):
        self.dean_one_b = dean
    def dean_print1(self):
        print(f"{self.dean_four_b}")
    def heads_Buss(self):
        print(f"The head of Buisness is ", end = "")
        print(self.chair_seven.heads7_print())
       
        print(f"The head of Digital Media is ", end = "")
        print(self.chair_eight.heads8_print())

class Wizardy:

    def __init__(self, dean):
        self.dean_one_b = dean
    def dean_print1(self):
        print(f"{self.dean_five_b}")
    def heads_Wiz(self):
        print(f"The head of Spells is ", end = "")
        print(self.chair_nine.heads9_print())
       
        print(f"The head of Potions is ", end = "")
        print(self.chair_ten.heads10_print())

class Heads:
    def __init__(self, head1, head2, head3, head4, head5, head6, head7, head8, head9, head10):
        self.head_one = head1
        self.head_two = head2
        self.head_three = head3
        self.head_four = head4
        self.head_five = head5
        self.head_six = head6
        self.head_seven = head7
        self.head_eight = head8
        self.head_nine = head9
        self.head_ten = head10
    def heads1_print(self):
        print(f"{self.head_one}")
    def heads2_print(self):
        print(f"{self.head_two}")
    def heads3_print(self):
        print(f"{self.head_three}")
    def heads4_print(self):
        print(f"{self.head_four}")
    def heads5_print(self):
        print(f"{self.head_five}")
    def heads6_print(self):
        print(f"{self.head_six}")
    def heads7_print(self):
        print(f"{self.head_seven}")
    def heads8_print(self):
        print(f"{self.head_eight}")
    def heads9_print(self):
        print(f"{self.head_nine}")
    def heads10_print(self):
        print(f"{self.head_ten}")


faculity = TSC("Pres Idint", "Scin Ark", "Eden Cathrine", "Nurswell Wells", "Bunson Meda", "Albus Dumbledore", "John Smith", "Paul Adam", "Betsy Zoe", "Simon Peter", "James Maddison", "Page Turner", "Max Scott", "Daniel Goodman", "Harry Potter", "Severus Snape")

faculity.pres()
faculity.deans()

错误原因分析

  1. 核心错误:给TSC类传入的Dean1到Dean5都是字符串(比如"Scin Ark"),但TSC的deans方法里调用了self.dean_one.dean_print()——字符串对象没有dean_print方法,直接触发报错。
  2. 属性名不匹配:多个学院类的dean_print1方法引用了未初始化的属性,比如Nursing类__init__里定义的是self.dean_one_b,但dean_print1里用了self.dean_three_b,会导致AttributeError。
  3. 未初始化chair属性:各学院的heads_*方法里引用的chair_one、chair_two等属性,没有在学院类的__init__中定义。
  4. Chair参数是字符串:TSC类接收的Chair1到Chair10都是字符串,不是Heads类实例,后续调用heads_print方法也会报错。

修复后的代码

class TSC:
    def __init__(self, president, arts_sci_dean, edu_dean, nursing_dean, business_dean, wizardy_dean, heads):
        self.president = president
        self.arts_sci = arts_sci_dean
        self.edu = edu_dean
        self.nursing = nursing_dean
        self.business = business_dean
        self.wizardy = wizardy_dean
        self.heads = heads

        # 将heads实例传入各学院
        self.arts_sci.set_chairs(self.heads)
        self.edu.set_chairs(self.heads)
        self.nursing.set_chairs(self.heads)
        self.business.set_chairs(self.heads)
        self.wizardy.set_chairs(self.heads)

    def pres(self):
        print(f"The President of TSC is {self.president}")
   
    def deans(self):
        print(f"The dean of the school of Arts & Sciences is {self.arts_sci.dean_name}")
        print(f"The dean of the school of Education is {self.edu.dean_name}")
        print(f"The dean of the school of Nursing and Wellness is {self.nursing.dean_name}")
        print(f"The dean of the school of Business and Digital Media is {self.business.dean_name}")
        print(f"The dean of the school of Wizardry is {self.wizardy.dean_name}")

    def all_heads(self):
        self.arts_sci.print_heads()
        self.edu.print_heads()
        self.nursing.print_heads()
        self.business.print_heads()
        self.wizardy.print_heads()


class Arts_and_Sciences:
    def __init__(self, dean_name):
        self.dean_name = dean_name
        self.chairs = None
    
    def set_chairs(self, heads):
        self.chairs = heads
    
    def print_heads(self):
        print(f"The head of Art is {self.chairs.head_one}")
        print(f"The head of Science is {self.chairs.head_two}")


class Education:
    def __init__(self, dean_name):
        self.dean_name = dean_name
        self.chairs = None
    
    def set_chairs(self, heads):
        self.chairs = heads
    
    def print_heads(self):
        print(f"The head of Education is {self.chairs.head_three}")
        print(f"The head of Teaching Skills is {self.chairs.head_four}")


class Nursing:
    def __init__(self, dean_name):
        self.dean_name = dean_name
        self.chairs = None
    
    def set_chairs(self, heads):
        self.chairs = heads
    
    def print_heads(self):
        print(f"The head of Nursing is {self.chairs.head_five}")
        print(f"The head of Wellness is {self.chairs.head_six}")


class Business:
    def __init__(self, dean_name):
        self.dean_name = dean_name
        self.chairs = None
    
    def set_chairs(self, heads):
        self.chairs = heads
    
    def print_heads(self):
        print(f"The head of Business is {self.chairs.head_seven}")
        print(f"The head of Digital Media is {self.chairs.head_eight}")


class Wizardy:
    def __init__(self, dean_name):
        self.dean_name = dean_name
        self.chairs = None
    
    def set_chairs(self, heads):
        self.chairs = heads
    
    def print_heads(self):
        print(f"The head of Spells is {self.chairs.head_nine}")
        print(f"The head of Potions is {self.chairs.head_ten}")


class Heads:
    def __init__(self, art_head, science_head, edu_head, teaching_head, nursing_head, wellness_head, business_head, media_head, spells_head, potions_head):
        self.head_one = art_head
        self.head_two = science_head
        self.head_three = edu_head
        self.head_four = teaching_head
        self.head_five = nursing_head
        self.head_six = wellness_head
        self.head_seven = business_head
        self.head_eight = media_head
        self.head_nine = spells_head
        self.head_ten = potions_head


# 创建各学院实例
arts_sci = Arts_and_Sciences("Scin Ark")
edu = Education("Eden Cathrine")
nursing = Nursing("Nurswell Wells")
business = Business("Bunson Meda")
wizardy = Wizardy("Albus Dumbledore")

# 创建系主任实例
heads = Heads(
    "John Smith", "Paul Adam", "Betsy Zoe", "Simon Peter",
    "James Maddison", "Page Turner", "Max Scott", "Daniel Goodman",
    "Harry Potter", "Severus Snape"
)

# 创建TSC大学实例
faculty = TSC("Pres Idint", arts_sci, edu, nursing, business, wizardy, heads)

# 输出内容
faculty.pres()
faculty.deans()
faculty.all_heads()

修复说明

  1. 重构TSC初始化参数:不再接收大量字符串,而是接收各学院实例和Heads实例,避免字符串调用类方法的错误。
  2. 统一属性命名:修正各学院类的属性名不匹配问题,用dean_name统一存储院长姓名。
  3. 添加set_chairs方法:给各学院传入Heads实例,让学院类可以访问系主任信息。
  4. 简化打印逻辑:直接用字符串格式化输出,避免嵌套print导致的重复换行。
  5. 优化类结构:减少冗余代码,让类职责更清晰。

内容的提问来源于stack exchange,提问作者Martha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 12:25:21