+ 1

Question relating to classmethod.

I failed a coding test. I don't know the exact question, but it's something this. '''input1= int input2= int[]''' class something(object): @classmethod def findlargesmalldiff(cls,input1,input2): pass Need to fill the required space with correct code. Eg. input1= 5 input2= [11,13,7,9,15] expected output: 7 I totally messed up by doing the below code and felt like my python skills sucks. class something(object): self.input1= input1 self.input2= input2 def number(self): return min(input2) input1= 5 input2= [] input2.append(input1) @classmethod def findlargesmalldiff(cls,input1,input2): return cls(input2) diff= something.findlargesmalldiff(5,['7','11','13','9','12']) print(diff.number())

16th Mar 2019, 11:26 AM
Prithvi
4 Answers
+ 4
Hi. First, the point of defining a classmethod, is that you can access it from outside the class without actually instantiating your class. So instead of doing this: inst = something() result = inst.findlargestsmalldiff(params) .. You can simply call the method prefixing the classname: result = something.findlargestsmalldiff(params) Same way as for example you can use math.sqrt() So you do not need to define instance variables inside your class (typically you assign a value to those in the constructor method, like: self.var1 = param1) You don't need to refer to self anywhere. You may not even refer to cls either, unless you want to use a static class variable or another classmethod of the same class. Just write all the program logic inside the classmethod and make sure it returns the desired value. Can you describe with your own words what is the expected result of the findlargesmalldiff function?
16th Mar 2019, 3:39 PM
Tibor Santa
Tibor Santa - avatar
+ 2
This would be my solution based on the info you gave. Assuming the function has 2 parameters, a number and a list of numbers, and the return value is the element of the list which has the smallest difference compared to the number argument. https://code.sololearn.com/cz2kO8RVAYfn/?ref=app
17th Mar 2019, 1:45 AM
Tibor Santa
Tibor Santa - avatar
+ 2
You do not need to declare the input variables anywhere inside the class, because they are passed as parameter to the function. You do not need to refer to self or cls. Just treat the classmethod function, as if it were a completely standalone function, without a class. It has inputs, it does some calculation and it returns output. That's all there is to it :)
17th Mar 2019, 11:14 AM
Tibor Santa
Tibor Santa - avatar
+ 1
Oh Wow! Tibor Santa I am still quite doubtful at some of the code though, but I understood the code. Thanks a lot for your Answer. I am a part happy for your answer and unhappy for my little confusion. There's so much to remember, man! Can the code be made little more simpler, as you mentioned that the code can be written in different way too. And can you mention where I went wrong. I am all jumbled when I did the code. This is what I did before. class something(object):     self.input1= input1     self.input2= input2         def number(self):         return min(input2)             input1= 5     input2= []     input2.append(input1)     @classmethod     def findlargesmalldiff(cls,input1,input2):         return cls(input2) diff= something.findlargesmalldiff(5,['7','11','13','9','12']) print(diff.number())
17th Mar 2019, 5:12 AM
Prithvi