简体   繁体   English

如何在 class Z23EEEB4347BDD265DDFC6B7EE9A3B7 中的另一个 function 中使用一个 function 变量

[英]How to use one function variables inside another function in class python

I've code like this我有这样的代码

class salesperson:

    def __init__(self, name):
        self.name=name
    
    
    def buyproduct(self, product_name, market_price, quantity):
        self.product_name = product_name
        self.market_price=market_price
        self.quantity=quantity
    
        return ({self.product_name: [self.market_price, self.quantity]})


    
    
    def getname(self):
        print(self.name)

Input:输入:

sp_name=salesperson('name')
sp_name.buyproduct('mobile',30,20)

output: output:

{'mobile': [30, 20]}

Know I want to create a separate function called "getvalue" inside class & Use that variables in "buyproduct" function & want to perform so operations.知道我想在 class 中创建一个名为“getvalue”的单独 function 并在“购买产品”function 中使用该变量并希望执行此类操作。

Like.喜欢。

class salesperson:


    def __init__(self, name):
        self.name=name
    
    
    def buyproduct(self, product_name, market_price, quantity):
        self.product_name = product_name
        self.market_price=market_price
        self.quantity=quantity
    
        return ({self.product_name: [self.market_price, self.quantity]})

####################################################creating new func######
    def getvalue(self):
        self.buyproduct()
        finalamount=market_price * quantity
        return finalamount
##################################################################
    
    
    def getname(self):
        print(self.name)

Clearly I don't know the syntax, So can you guys know the syntax/example that calls the one function inside another function with in class?显然我不知道语法,所以你们能知道在另一个 function 和 class 中调用一个 function 的语法/示例吗?

Expected input:预期输入:

sp_name.getvalue() 

should print应该打印

Expected output预期 output

600

Explanation: 30 * 20 = 600(From buy product function)...解释:30 * 20 = 600(来自购买产品功能)...

Thanks in advance提前致谢

I'm pretty sure you just can't do it that way.我很确定你不能那样做。 The way you are supposed to do this in Python is put all the class variables in the init function.您应该在 Python 中执行此操作的方式是将所有 class 变量放入 init function 中。 So you would do like this:所以你会这样做:

class Salesperson:


    def __init__(self, name, product_name, market_price, quantity):
        self.name = name
        self.product_name = product_name
        self.market_price = market_price
        self.quantity = quantity
    
    def buy_product(self):
        return {self.product_name: [self.market_price, self.quantity]}

    def get_value(self):
        finalamount=self.market_price * self.quantity
        return finalamount

    def getname(self):
        print(self.name)

And if your problem is you sometime want to execute get_value but you dont want to give useless variables to Salesperson class, you just give name and product_name None value, like this:如果您的问题是您有时想执行 get_value 但又不想给销售人员 class 提供无用的变量,您只需提供 name 和 product_name None 值,如下所示:

sp_name=Salesperson(None,None,30,20)
sp_name.get_value()

If you really want to do it your way (which I don't recommend), return in buy_product the original dictionary + the variables you need in get_value:如果您真的想按照自己的方式进行操作(我不推荐),请在 buy_product 中返回原始字典 + 在 get_value 中需要的变量:

class Salesperson:


    def __init__(self, name):
        self.name = name
    
    def buy_product(self, product_name, market_price, quantity):
        return [{product_name: [market_price, quantity]}, market_price, quantity]

    def get_value(self):
        buy_product_returns = self.buy_product()
        finalamount = buy_product_returns[1] * buy_product_returns[2]
        return finalamount

    def getname(self):
        print(self.name)

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

相关问题 如何在另一个类的类的函数内部使用变量-Python - How to use a variable that inside of a function of a class in another class - Python Python:如何在另一个函数中的一个函数中使用变量 - Python: How to use variables in a function in another function 如何将一个class的function变量用于Python中的另一个class - How to use function variable of one class to another class in Python 如何在另一个函数中使用在一个函数中设置的变量 - How to use variables set in one function in another Python:在 Tkinter 应用程序中使用从一个函数到另一个函数的变量(文件名) - Python: Use of variables (filename) from one function to another in Tkinter application 如何在不传递参数的情况下在另一个函数中使用函数变量? - How to use function variables in another function without passing as parameter in Python? 你如何在一个类中的另一个函数中使用一个函数? - How do you use a function in a class, inside another function? 如何在Python中的类中使用另一个函数 - How to use one function in another with classes in Python 如何将 function 的变量用于另一个变量? (Python) - How to use variable of function for another one? (Python) 在python类中,如何在另一个函数中使用一个函数定义的变量? - In python class, how can I use the variable defined by one function in another function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM