+ 1

Methods in Python like Java and how to use it

in java you can declare a method for example public void method () {} and you can call it using this or method (), but how can this be done in python? Sorry for asking something simple but I am starting with this language.

20th Oct 2019, 4:48 AM
Amaury Villanueva Sosa
Amaury Villanueva Sosa - avatar
3 Answers
+ 2
In python you have to have the method code above the method that calls it, otherwise it won't work. You declare the method with "def" followed by the method name and parameters, you dont have to declare the return type, public/private/protected or static. Does python have those types of things? I don't know, probably, I'm new to python as well. But you do call the method the same as Java. https://code.sololearn.com/cO0105ggX77M/?ref=app
20th Oct 2019, 5:07 AM
Odyel
Odyel - avatar
0
Functions work very similarly in almost any programming language. 1: You define it with name, certain amount of named parameters and a codeblock. 2: You call the function with the function name and give it the wanted amount of arguments and the code block will run. 3: A value will be returned to the position where the function was called, (unless if the function has void as return type). The main difference between functions in Python and Java is that in Python functions won't get a specified return type.
20th Oct 2019, 6:12 PM
Seb TheS
Seb TheS - avatar
0
Thank you :3
22nd Oct 2019, 2:54 AM
Amaury Villanueva Sosa
Amaury Villanueva Sosa - avatar