Explain me this snippet pls | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Explain me this snippet pls

Hello everyonen could someone please explain me how this snippet work? when i open cmd and launch my script with python i can see something happened but can't explain why.. here is the part i don't understand: if __name__=='__main__': import sys pluto=Dog() if len(sys.argv)>1: pluto.sound=sys.argv[1] print(pluto.make_sound()) to look at the full code please check this link : https://code.sololearn.com/c0UbA9o1NLq6 thanks you in advence for your help ;)

1st Aug 2020, 9:39 PM
Maximilien
Maximilien  - avatar
2 Answers
+ 3
So if you run your file from cmd and pass addition arguments like: python3 Animal.py meow sys.argv makes a list of ['Animal.py','meow'] and then if condition checks it's length which in this case is greater than 1 ,so second argument(meow) is assigned to Object pluto sound property and printed thereafter ,but if no additional argument is passed barks get printed
1st Aug 2020, 10:03 PM
Abhay
Abhay - avatar
+ 1
thanks for this nice explanation ! ;)
2nd Aug 2020, 11:04 AM
Maximilien
Maximilien  - avatar