[英]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.