Python Forum
Parenthesis in User-Defined Functions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Parenthesis in User-Defined Functions
#1
I have the following question regarding using parenthesis when calling use defined function.

This is the code proposed by the book to simulate white noise process for 100 periods using User-Defined Function.

def generate_data(n, generator_type):
    ϵ_values = []
    for i in range(n):
        e = generator_type()
        ϵ_values.append(e)
    return ϵ_values

data = generate_data(100, np.random.uniform)
plt.plot(data)
plt.show()
I am trying to change this code in the following way:

def generate_data(n, generator_type):
    ϵ_values = []
    for i in range(n):
        e = generator_type
        ϵ_values.append(e)
    return ϵ_values

data = generate_data(100, np.random.uniform())
plt.plot(data)
plt.show()
So, instead of having parenthesis inside the body of the function (e=generator_type()) I provide it as the name of the second variable in the function generate_data. But the code does not run in the second case. I wonder what is the difference and why it fails to provide the same results as the first version. The call should just take np.random.uniform() instead of generator_type and produce the same result should not it? Why should I put generator_type() parenthesis in the body and then put np.random.uniform as the variable? why does it make a difference where to put ()?
Reply
#2
The difference is that in the first case the function generator_type() is called for every index in the loop for i in ramge... while in the second case it is called only once.
Reply
#3
data = generate_data(100, np.random.uniform)
Here the function np.random.uniform is given as a parameter to the function
and inside the function the according argument name generator_type can be used to call the function by adding ()
data = generate_data(100, np.random.uniform())
Here you are CALLING the function np.random.uniform once and that call returns a random value
and that value is passed as a parameter to the function and then assigned to e and then to the ϵ_values list.
So your 2nd code snippet runs but all values are the same.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  User-defined function to reset variables? Mark17 3 1,589 May-25-2022, 07:22 PM
Last Post: Gribouillis
  Multiple user defined plots with secondary axes using for loop maltp 1 1,393 Apr-30-2022, 10:19 AM
Last Post: maltp
  Definitions in User-Created Functions and For Loops new_coder_231013 6 2,027 Dec-29-2021, 05:51 AM
Last Post: ndc85430
  Why built in functions are defined as class? quazirfan 5 2,713 Oct-23-2021, 01:20 PM
Last Post: Gribouillis
  Why does Python not use parenthesis to contain loops? davidorlow 3 3,399 Jun-09-2021, 06:33 AM
Last Post: Gribouillis
  Running scripts and location of saved interpreted user-defined classes and functions leodavinci1990 3 2,466 Aug-25-2020, 03:43 AM
Last Post: micseydel
  User functions don't work: Baldev10 7 3,082 Aug-18-2020, 08:34 PM
Last Post: deanhystad
  python library not defined in user defined function johnEmScott 2 3,768 May-30-2020, 04:14 AM
Last Post: DT2000
  Problem with user defined main menu function stefzeer 3 2,341 Mar-27-2020, 06:12 AM
Last Post: buran
  User defined functions inside other user defined functions WildP 1 1,916 Jan-29-2020, 04:57 PM
Last Post: Clunk_Head

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020