![]() |
problem with function return value - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: problem with function return value (/thread-138.html) |
problem with function return value - ujjwalrathod007 - Sep-22-2016 I ave some code which looks something as follows... class xyz: def anything(self): if condition: if condition: return something else: return somethingelse elif Condition: return something else: return something def calculations(self): x= something+5 y= somethingelse+10I dont know what the function anything will return weather something or somethingelse.. Latter I want to use it in another function calculations.. it sould only update that value and rst should be the same..!! Any idea?? RE: problem with function return value - Ofnuts - Sep-22-2016 Unclear... I don't see where your code calls anything(). RE: problem with function return value - ujjwalrathod007 - Sep-22-2016 sorry for that but I take an object of a class xyz as following class xyz: def anything(self): if condition: if condition: return something else: return somethingelse elif Condition: return something else: return something def calculations(self): x= something+5 y= somethingelse+10 object1=xyz object1.anything() object1.calculations()it should now take "something" or "somethingelse " depending on the conditions given in multiple if-else loops and do the calculations.. in short, I dont know what the function anything() will return.. depending on that I only want to change variables in calculations() i have done the following changes to the code to execute it properly... ''' Created on 22 Sep 2016 @author: rathodul ''' something=10 somethingelse=0 a=2 class xyz: def anything(self): if something<somethingelse: if a==10: return something else: return somethingelse elif a>somethingelse: return something else: return something def calculations(self): x= something+5 y= somethingelse+10 return x,y object1=xyz() object1.anything() object1.calculations() print object1.calculations() RE: problem with function return value - wavic - Sep-22-2016 Your can't start to write a code just supposing how to do it and then asking why it's not working. Start here. See how to define a class, how to create an instance of a class. How to pass the parameters. RE: problem with function return value - ujjwalrathod007 - Sep-22-2016 Hello, I haven’t supposed the code but I cant right the whole code because it is protected. RE: problem with function return value - Ofnuts - Sep-22-2016 Still unclear... anything() doesn't change the state of the object. But computations() doesn't either, unless x and y are attributes? What is the problem with using: if object1.anything()==something: .... else: .... RE: problem with function return value - nilamo - Sep-22-2016 (Sep-22-2016, 02:37 PM)ujjwalrathod007 Wrote: I dont know what the function anything will return weather something or somethingelse..That depends entirely on the conditions. Which you never defined. So nothing would be returned, because the script is invalid. RE: problem with function return value - snippsat - Sep-22-2016 You are missing basic understanding how class works. So read more about it. Quote:I dont know what the function anything will returnIt's not called a function when it belong to class,it's now a method. You should not have variables outside the class, these should be arguments that is given to the class. Just to write something that make more sense,in the way class work. Not changing anything to method anything which could be wrong. class xyz: def anything(self, anything, somethingelse, a): if something < somethingelse: if a == 10: return something else: return somethingelse elif a > somethingelse: return something else: return something def calculations(self, any_obj): x = any_obj + 5 y = any_obj + 10 return x, y object1 = xyz() something = 10 somethingelse = 0 a = 2 any_obj = object1.anything(something, somethingelse, a) print(object1.calculations(any_obj)) #(15, 20) RE: problem with function return value - ujjwalrathod007 - Sep-23-2016 True, Is there any way to check weather the method has return Something or Somethingelse. Because I do this in iteration.. While true: loop. If method returns only something then I only want to update Something and keep Somethingelse as it is..!! and vice-versa. RE: problem with function return value - nilamo - Sep-23-2016 Yeah. Check the return value in whatever's calling the method. |