简体   繁体   English

如何在同一个 class 内的另一种方法中以一种方法调用变量?

[英]How do I call the variable in one method in another method inside the same class?

I'm very new to Python and I use Spyder to do my Python homework.我对 Python 非常陌生,我使用 Spyder 来完成我的 Python 作业。 I am making a code that requires using class to find out a student's average marks.我正在编写一个代码,需要使用 class 来找出学生的平均分数。 Here's my code:这是我的代码:

class Student:
    def __init__(self,name='default student'):
        if name==None:
            self.name='default student'
        elif name!=None:
            self.name=name
    def quizcalc(self,*marks):
        s=0
        for x in marks:
            s+=x
        return s/3
    def printdetail(self):
        print("Hello ",self.name)
        print("Your average quiz score is ",self.quizcalc) #something is not right in this line
s1 = Student()
s1.quizcalc(10)
print('--------------------------------')
s1.printdetail()
s2 = Student('Harry')
s2.quizcalc(10,8)
print('--------------------------------')
s2.printdetail()
s3 = Student('Hermione')
s3.quizcalc(10,9,10)
print('--------------------------------')
s3.printdetail()

and this is what the output looked like:这就是 output 的样子:

--------------------------------
Hello  default student
Your average quiz score is  <bound method Student.quizcalc of <__main__.Student object at 0x000001FDD65AEA48>>
--------------------------------
Hello  Harry
Your average quiz score is  <bound method Student.quizcalc of <__main__.Student object at 0x000001FDD65AE708>>
--------------------------------
Hello  Hermione
Your average quiz score is  <bound method Student.quizcalc of <__main__.Student object at 0x000001FDD65AE348>>

but my desired output is this:但我想要的 output 是这样的:

--------------------------------
Hello default student
Your average quiz score is 3.3333333333333335
--------------------------------
Hello Harry
Your average quiz score is 6.0
--------------------------------
Hello Hermione
Your average quiz score is 9.666666666666666 

You just need to add parenthesis你只需要添加括号

self.quizcalc()

Without parenthesis you're just printing the function itself instead of the result of running that function.如果没有括号,您只是打印 function 本身,而不是运行 function 的结果。


And for fun, let me illustrate a scenario where you might actually want to print out the function name like you were doing.为了好玩,让我举例说明一个场景,您可能真的想像以前那样打印出 function 名称。

So lets say you have the class所以假设你有 class

class Student:
    def quizcalc_integer(self, *marks):
        pass # code goes here
    def quizcalc_decimal(self, *marks)
        pass # code goes here

    def printdetail(self, quiz_function):
        print("Hello ",self.name)
        print("Your average quiz score is ",quiz_function()) 

s1 = Student()

And if you're working with decimal numbers you can do如果你正在使用十进制数字,你可以这样做

s1.printdetail(s1.quizcalc_integer)

And if they are not decimals如果它们不是小数

s1.printdetail(s1.quizcalc_decimal)

Very fictitious example, but if you are trying to debug what is going on and you want to know what function is stored inside of the variable quiz_function , you would then have a reason to do print(quiz_function) instead of print(quiz_function()) .非常虚构的示例,但是如果您尝试调试正在发生的事情并且想知道 function 存储在变量quiz_function中,那么您将有理由执行print(quiz_function)而不是print(quiz_function()) .

And that is my long unnecessary answer to why it's even possible to print the name of a function in the first place.这就是我对为什么首先可以打印 function 的名称的长期不必要的回答。

If you are allowed to change class strucure then you can do below changes:如果您被允许更改 class 结构,那么您可以进行以下更改:

  1. create instance level variable average.创建实例级变量平均值。
  2. instead of returning value from quizcalc assign calcuated result to self.average.而不是从 quizcalc 返回值,而是将计算结果分配给 self.average。
  3. Inside printdetail() print instance variable self.average.在 printdetail() 内部打印实例变量 self.average。 Please check below code:请检查以下代码:
class Student:
    def __init__(self,name='default student'):
        self.average=0.0 #1st change
        if name==None:
            self.name='default student'
        elif name!=None:
            self.name=name
    def quizcalc(self,*marks):
        s=0
        for x in marks:
            s+=x
        self.average=s/3  #2nd change
    def printdetail(self):
        print("Hello ",self.name)
        print("Your average quiz score is ",self.average)#3rd change
s1 = Student()
s1.quizcalc(10)
print('--------------------------------')
s1.printdetail()
s2 = Student('Harry')
s2.quizcalc(10,8)
print('--------------------------------')
s2.printdetail()
s3 = Student('Hermione')
s3.quizcalc(10,9,10)
print('--------------------------------')
s3.printdetail()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据同一 class 中另一个方法的输入调用 class 中的任何方法? - How can I call any method inside a class depending on an input to another method in the same class? 如何在同一类的另一个方法中调用方法的变量 - How to call a variable of a method in another method of the same class 如何修改我在 class 的另一种方法中创建的 pandas dataframe? - How do I modify a pandas dataframe I created in one method of a class in another method of the same class? 如何在同一个 class 的另一个 api 中调用 python api 方法? - How to call python api method inside another api in same class? 如何在同一个类中从一个方法访问我的变量? - how to access my variable from one method to another in same class? 如何在没有实例的情况下调用同一类的另一个方法? - How can I call another method of the same class without an instance? 如何从另一个 python 文件访问 class 方法中的变量? - How do I access the variable inside a class method from another python file? 如何在基于django类的视图中从同一个类中的另一个方法访问一个方法的变量 - how to access variable of one method from another method within the same class in django class based views 如何从 Python 中的另一个文件调用类方法? - How do I call a class method from another file in Python? 如何从同一个类中的另一个方法访问一个类中的方法中的变量 - How to access a variable in a method in a class from another method in the same class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM