Function call in Python
Check the following code:
Interesting part of the code is
def fibonacci(x):
if x < 2: return 1
return (fibonacci(x-2) + fibonacci(x-1))
def factorial(x):
if x < 2: return 1
return (x * factorial(x-1))
def main():
funcs = [fibonacci, factorial]
n = 10
for i in range(len(funcs)):
print funcs[i](n)
main()
Interesting part of the code is
print funcs[i](n)
. Sometimes it is useful to call the function in this way.
Comments
That last for loop in main could be:
| for func in funcs:
| print func(n)
You can iterate over the list saving the complication of indexing.
- Paddy.
how to know a language is scripting or even interpreted??
Ans: Check if it has any function like eval(). which dynamically executes code inside a string !!!
i'm a new in python,,,
my problem is, can you give a simple codes in inputting first the positive integer then output the factorial of the inputted numbers...
Can you tell me what's wrong with this:
class xyz:
def fibonacci(self, x):
if x < 2: return 1
return (self.fibonacci(x-2) + self.fibonacci(x-1))
def factorial(self, x):
if x < 2: return 1
return (x * self.factorial(x-1))
funcs = [fibonacci, factorial]
def w(self):
return self.funcs
if __name__ == "__main__":
funcs2 = xyz().w()
n = 10
for i in range(len(funcs2)):
print xyz().funcs2(n)