Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将自变量传递到函数中_Python_Oop - Fatal编程技术网

Python 将自变量传递到函数中

Python 将自变量传递到函数中,python,oop,Python,Oop,如何将参数(a,b,c)传递给二次公式的函数,而不必在函数中重新定义它们?我知道我可以在公式中使用self.a而不是a(与b和c相同),但是我如何将参数self.a作为a,self.b作为b和self.c传递到函数中 class Calc: def __init__(self, a, b, c): self.a = a self.b = b self.c = c def quadraticformula(self):

如何将参数(
a
b
c
)传递给二次公式的函数,而不必在函数中重新定义它们?我知道我可以在公式中使用
self.a
而不是
a
(与
b
c
相同),但是我如何将参数
self.a
作为
a
self.b
作为
b
self.c
传递到函数中

class Calc:

    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c
    
    def quadraticformula(self):
        c = self.c
        b = self.b 
        a = self.a
        
        neg = ((b*-1)-(sqrt((b**2)-4*a*c)))/(2*a)
        pos = ((b*-1)+(sqrt((b**2)-(4*a*c))))/(2*a)
        return (pos,neg)

你不必重新定义任何东西。
\uuuu init\uuuu
方法允许该类的所有其他方法能够访问该变量。因此,一旦您在
\uuuu init\uuuu
方法中实际定义了一个传递给类的变量(您将其作为函数引用,而它不是函数引用),您所要做的就是用您需要的任何操作引用它

# within you quadraticformula method
...
neg = ((self.b*-1)-(sqrt(self.b**2 - 4*self.a*self.c)))/(2*self.a)
pos = ((self.b*-1)+(sqrt(self.b**2 - 4*self.a*self.c)))/(2*self.a)
return pos, neg
将属性传递给类时,您已创建该类的实例,如下所示:

a = # something
b = # something
c = # something

cl = Calc(a, b, c)
cl.quadraticformula() # call the method (a function with a method) of the function here

# You can call this method in the __init__ method if you want to 
# execute as soon as you call the class instead of using the instance 
# to reference it
class Calc:
  def __init__(self,a,b,c):
     self.a = a
     self.b = b
     self.c = c
     self.quadraticformula

你不必重新定义任何东西。
\uuuu init\uuuu
方法允许该类的所有其他方法能够访问该变量。因此,一旦您在
\uuuu init\uuuu
方法中实际定义了一个传递给类的变量(您将其作为函数引用,而它不是函数引用),您所要做的就是用您需要的任何操作引用它

# within you quadraticformula method
...
neg = ((self.b*-1)-(sqrt(self.b**2 - 4*self.a*self.c)))/(2*self.a)
pos = ((self.b*-1)+(sqrt(self.b**2 - 4*self.a*self.c)))/(2*self.a)
return pos, neg
将属性传递给类时,您已创建该类的实例,如下所示:

a = # something
b = # something
c = # something

cl = Calc(a, b, c)
cl.quadraticformula() # call the method (a function with a method) of the function here

# You can call this method in the __init__ method if you want to 
# execute as soon as you call the class instead of using the instance 
# to reference it
class Calc:
  def __init__(self,a,b,c):
     self.a = a
     self.b = b
     self.c = c
     self.quadraticformula

不要使用带有构造函数的类,一般只使用普通函数

def calc(a, b, c):
    neg = ((b*-1)-(sqrt(b**2 - 4*a*c)))/(2*a)
    pos = ((b*-1)+(sqrt(b**2 - 4*a*c)))/(2*a)
    return pos, neg
然后调用函数:

>>> calc(1, 2, -3)
(1.0, -3.0)

不要使用带有构造函数的类,一般只使用普通函数

def calc(a, b, c):
    neg = ((b*-1)-(sqrt(b**2 - 4*a*c)))/(2*a)
    pos = ((b*-1)+(sqrt(b**2 - 4*a*c)))/(2*a)
    return pos, neg
然后调用函数:

>>> calc(1, 2, -3)
(1.0, -3.0)

作为一个班级有什么意义?如果将参数从
\uuu init\uuu
移动到
quadraticformula
,则根本没有实例状态。而是使用
定义quadraticformula(self,a,b,c):
@Sociopath,但这样类就没有意义了,您不需要将它们传递到函数中。创建
Calc
对象时传递它们
c=Calc(1,2,3)
c.quadraticformula()BTW这让我想到了如何更容易地做加减,最后我发表了文章。在这种情况下,您可以执行
s=sqrt(b**2-4*a*c);pos,neg=(-b+i)/(2*a)表示(s,-s)中的i)
。作为一个类有什么意义?如果将参数从
\uuu init\uuu
移动到
quadraticformula
,则根本没有实例状态。而是使用
定义quadraticformula(self,a,b,c):
@Sociopath,但这样类就没有意义了,您不需要将它们传递到函数中。创建
Calc
对象时传递它们
c=Calc(1,2,3)
c.quadraticformula()BTW这让我想到了如何更容易地做加减,最后我发表了文章。在这种情况下,您可以执行
s=sqrt(b**2-4*a*c);pos,neg=(-b+i)/(2*a)表示(s,-s)中的i)
。比如,“这不应该是一个类”的签名是它有两个方法,其中一个是
\uuuu init\uuuu
,“这不应该是一个类”的签名是它有两个方法,其中一个是
\uuuu init\uu