在Python中只包含函数的一部分

在Python中只包含函数的一部分,python,oop,Python,Oop,我有两个类继承自同一父类。两者都有一个只有部分共同的功能。例如,两者都打印三次Hello,但是Daughter1每次都会打印World,而Daughter2每次都会打印计数器。我现在就是这样做的 class Mother(): def foo(self): for self.i in range(3): print('Hello') self.bar() def bar(self): print('T

我有两个类继承自同一父类。两者都有一个只有部分共同的功能。例如,两者都打印三次
Hello
,但是
Daughter1
每次都会打印
World
,而
Daughter2
每次都会打印计数器。我现在就是这样做的

class Mother():
    def foo(self):
        for self.i in range(3):
            print('Hello')
            self.bar()
    def bar(self):
        print('This is just a dummy method')

class Daughter1(Mother):
    def bar(self):
        print('World')

class Daughter2(Mother):
    def bar(self):
        print(self.i)

d1 = Daughter1()
d1.foo()

d2 = Daughter2()
d2.foo()
它可以工作,但我发现它容易混淆和出错。有更好的方法吗

另一种选择是在子对象的方法中实现循环

class Mother():
    def foo(self):
        print('Hello')

class Daughter1(Mother):
    def bar(self):
        for i in range(3):
            self.foo()
            print('World')

class Daughter2(Mother):
    def bar(self):
        for i in range(3):
            self.foo()
            print(i)

d1 = Daughter1()
d1.bar()
这里的问题是,真正的循环相当复杂,所以我宁愿只编写一次

编辑:我将尝试提供更多的上下文。我稍后会添加代码,因为它现在不工作。 我正在尝试编写一个代码来创建随机考试。代码将 -记下联系人列表 -做一个考试模板 -创建带有随机数的考试版本 -创建PDF考试(使用latex) -计算并存储每个学生的答案 -给每个学生发一封电子邮件

我想把它用于不同的主题和不同的主题。因此,我决定创建一个父类
Exam
,并为从中继承的每个考试创建一个新类(例如统计考试
)。
Exam
负责:
-循环浏览联系人列表
-调用一个函数
create_-exam()`对于每个子函数都是不同的。 -创建PDF -清理乳胶的所有副产品。 -给每个学生发一封电子邮件

以一种非常简单的方式,它将像这样工作

import os.path as path
import pandas as pd

class Exam():
    def all_exams(self):
        contacts = pd.read_csv(path.join('contacts',self.grade + 'contacts.txt'))

        for _, person in contacts.iterrows(): 
            self.lastName = person['last name'] # Will be used in the daughter class
            self.firstName = person['first name']  # Will be used in the daughter class

            self.create_exam()

            self.create_pdf()

            self.email()

    def create_exam(self):
        exam = self.exam_headig()
        exam = exam + 'This is a dummy exam\n'
        exam = exam + self.exam_end()

        self.exam = exam



class StatisticsExam(Exam):
    def create_exam(self):
        self.exam_heading()
        self.statistics_exercise1()
        self.exam_end()


exam = StatisticsExam('statistics','9A')
exam.all_exams()

也许从一开始这就是错误的方法…

你的第一个方法是好的

现在,或者,根据您试图实现的目标,您可以执行以下操作:

class Mother():

    bar = ''

    def foo(self):
        for self.i in range(3):
            print('Hello {}'.format(self.bar))


class Daughter1(Mother):
    bar = 'world'

class Daughter2(Mother):

    @property
    def bar(self):
        return self.i


d1 = Daughter1()
d1.foo()

d2 = Daughter2()
d2.foo()
对于一个简单的
打印
功能,它会让您感觉更加敏感。
正如您所看到的,它使用decorator输出
self.i

您的第一种方法是好的

现在,或者,根据您试图实现的目标,您可以执行以下操作:

class Mother():

    bar = ''

    def foo(self):
        for self.i in range(3):
            print('Hello {}'.format(self.bar))


class Daughter1(Mother):
    bar = 'world'

class Daughter2(Mother):

    @property
    def bar(self):
        return self.i


d1 = Daughter1()
d1.foo()

d2 = Daughter2()
d2.foo()
对于一个简单的
打印
功能,它会让您感觉更加敏感。
正如您所看到的,它使用decorator输出
self.i

您;没错,您的第一个实现很容易混淆/出错-如果没有其他实现,则在
Daughter2
中调用
bar
永远都不安全,除非其他实现已经设置了
self.i
。这将使测试、调试和记录变得很棘手

bar
方法打印类中定义的值或传递给该方法的值可能更清晰。这意味着子类的实现要简单得多。例如:

class Mother():
    def foo(self):
        for i in range(3):
            print('Hello')
            self.bar(i)
    def bar(self, val):
        if hasattr(self, 'print_val'):
            val = self.print_val
        print(val)

class Daughter1(Mother):
    def __init__(self):
        self.print_val = 'World'

class Daughter2(Mother):
    pass

d1 = Daughter1()
d1.foo()

d2 = Daughter2()
d2.foo()

你;没错,您的第一个实现很容易混淆/出错-如果没有其他实现,则在
Daughter2
中调用
bar
永远都不安全,除非其他实现已经设置了
self.i
。这将使测试、调试和记录变得很棘手

bar
方法打印类中定义的值或传递给该方法的值可能更清晰。这意味着子类的实现要简单得多。例如:

class Mother():
    def foo(self):
        for i in range(3):
            print('Hello')
            self.bar(i)
    def bar(self, val):
        if hasattr(self, 'print_val'):
            val = self.print_val
        print(val)

class Daughter1(Mother):
    def __init__(self):
        self.print_val = 'World'

class Daughter2(Mother):
    pass

d1 = Daughter1()
d1.foo()

d2 = Daughter2()
d2.foo()

这个简化的版本有点异味-我们可能需要查看完整的代码来了解您想要实现什么/它有什么不同。看起来像一个。如果你能提供更多的上下文,这肯定会有帮助。你的第一个版本是我编辑原始文章尝试添加更多上下文的一个例子。对不起,如果有点乱,我没有正式的培训OOP@match,我根据你的要求对问题进行了编辑,以提供更多的上下文。这就足够了,还是我应该添加更多?这个简化版本有点异味-我们可能需要查看完整的代码,以了解您想要实现什么/它有什么不同。看起来像一个。如果你能提供更多的上下文,这肯定会有帮助。你的第一个版本是我编辑原始文章尝试添加更多上下文的一个例子。对不起,如果有点乱,我没有正式的培训OOP@match,我根据你的要求对问题进行了编辑,以提供更多的上下文。这够了吗?还是我应该再加一点?