Unlike many other programming languages, such as Java, Python does not require the main method to be inside a class. Even more, Python does not need to have a main method defined: it runs the whole file as an application. In your original post, Python performs this actions:
- Define the method
singleNumber as the code it holds.
- Define the method
main as the code it holds.
- Save this two methods inside the class
Solution.
- No more lines to run, therefore ends application.
To make the application correct, you must write it as follows:
class Solution(object):
def singleNumber(self, nums):
for i in range(0,len(nums),2):
if (i != len(nums) - 1) and (nums[i] != nums[i+1]):
print (nums[i])
elif i == len(nums) - 1:
print (nums[i])
if __name__=='__main__':
nums=[1,1,2,2,3]
s=Solution()
s.singleNumber(nums)
print('done')
You may wonder why the line if __name__=='__main__':; every file contains the variable __name__ implicitly defined and its value depends if you're running the file directly or it is imported. On the first case, the assignment __name__='__main__' is done, on the second case the __name__ variable is assigned the name of the file itself; this gives you an idea whether this file is the main or not.
You may also discard the Solution class and promote the singleNumber method to a module method.
maina method ofSolution?