Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

Python: How to change the value of a function inside a class from an outside function

Hi I am showing a subsection of my code. I created a function(propertyValue) inside an object class(CashOnCashROI). I then created a function (calculator) outside the class. I instantiated the class as "rental" and then tried to use rental.propertyValue() to call the propertyValue function outside the class.

What I am trying to do is change the value for "self.globValue = int(value)" from the calculator() function but I keep getting: TypeError: propertyValue() takes 1 positional argument but 2 were given. I was hoping someone could help me explain what is going on, how to fix it, and if there is a better way to do this? I am just starting to learn about classes so I greatly appreciate it.

Here is the code, it should give the same TypeError.

class CashOnCashROI:      
    def propertyValue(self):
        value = input('How much will you pay for the property? 
')
        while value.isdigit() == False:
            value = input('Sorry, we need a number? What is the proposed property value? 
')
        print(f"Your current purchase value for the property is: {value}")
        self.globValue = int(value)

def calculator():
    rental = CashOnCashROI()
    while True:
        rental.propertyValue()
        choice = input('<Other parameters asked here>, "Value" to change the property value.
')
        if choice.lower() == "value":
            xvalue = input('What would the new property value be? 
')
            rental.propertyValue(xvalue) ### How do I change the value here???
question from:https://stackoverflow.com/questions/65867747/python-how-to-change-the-value-of-a-function-inside-a-class-from-an-outside-fun

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Modify the propertyValue function: Setting a new argument (value) allows to pass a value from outside the function as an argument and skip the input function. If no value is provided, use the old implementation:

class CashOnCashROI:      
    def propertyValue(self, value=None):
        if not value:
            value = input('How much will you pay for the property? 
')
            while value.isdigit() == False:
                value = input('Sorry, we need a number? What is the proposed property value? 
')
            print(f"Your current purchase value for the property is: {value}")
        self.globValue = int(value)

def calculator():
    rental = CashOnCashROI()
    while True:
        rental.propertyValue()
        choice = input('<Other parameters asked here>, "Value" to change the property value.
')
        if choice.lower() == "value":
            xvalue = input('What would the new property value be? 
')
            rental.propertyValue(xvalue) ### How do I change the value here???

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...