0

I've encountered the following code to create a user-defined function:

import numpy as np
import matplotlib.pyplot as plt

def generate_data(n):
    ϵ_values = []
    for i in range(n):
        e = np.random.randn()
        ϵ_values.append(e)
    return ϵ_values
data = generate_data(100)
plt.plot(data)
plt.show() #This is to plot

I don't understand the empty parentheses () anywhere in the code.

Student
  • 107
  • 6
  • 1
    This is how you invoke the function. – Guy Feb 12 '20 at 09:19
  • 1
    https://docs.python.org/3/reference/expressions.html#calls – bruno desthuilliers Feb 12 '20 at 09:28
  • @SergeBallesta indeed - but the official tutorial part on functions doesn't really explain this particular point. – bruno desthuilliers Feb 12 '20 at 09:33
  • @brunodesthuilliers: Not really, but the 4.6 paragraph of the tuto shows that if you use a function name without parens, you just get a ref to the function object, and the 4.7.1 shows an example of calling a function with no pararameters. I am sorry if my comment was read as offensive because it was not my intent, but I really think that trying to use numpy and matplot before knowing the basics of Python is dangerous. – Serge Ballesta Feb 12 '20 at 09:43
  • @SergeBallesta I'm definitly not the one to feel offended by your comment, which is just plain common sense as far as I'm concerned ;-) - my point was just that there were actually no _clear_ explanation on that exact point in the official tutorial, so nowhere we could linked to (hence my link to the language's grammar instead). – bruno desthuilliers Feb 12 '20 at 09:53

4 Answers4

2

Parentheses are for executing function, also adding parameters for it. If a function can work without any parameters, that parentheses could be empty.

tanaydin
  • 5,171
  • 28
  • 45
1

Empty parentheses mean that the function you are calling takes no arguments or it has default arguments pre-defined.

When a function is part of a class, we call it "method".

Example:

Python has a built-in class called str (string), to represent a sequence of characters. It has several methods (functions inside that class), one of which is isupper(). This method takes no arguments and will return True if the string is uppercase and False otherwise. Example:

"hello".isupper()  # this returns false.

What you are calling at the end of your code, plt.show() is a method (again, a function inside a class that takes no arguments, or has pre-defined defaults) of matplotlib.pyplot.

You can check the documentation to see all the available functions for pyplot https://matplotlib.org/3.1.1/api/pyplot_summary.html if you go there, click on any function, then click the [source] link, and you will see how the function is built.

Jir
  • 2,985
  • 8
  • 44
  • 66
Coroto
  • 11
  • 1
  • 4
0

Empty parentheses can be used to call function with default or no arguments - but a function itself can be accessed without using parentheses - just it will not do anything. Refer to Purpose of calling function without brackets python .

Siddharth
  • 373
  • 2
  • 17
0

Parentheses are used to invoke a function. Keeping () after any word means calling a function of that name. Say a function is

def fun(a, b):
  return a + b

This function, when called, will have 2 numbers inside (), like fun(2, 3). Similarly, say a function is

def fun2():
  print('Hello World')

This function can be called without passing any arguments, like fun2().

In your code, the first parentheses np.random.randn() means to call the randn function of random module in numpy package. Similarly, plt.show() means to call the show() function of matplotlib.pyplot module.

Swati Srivastava
  • 1,102
  • 1
  • 12
  • 18