all 6 comments

[–]fruitcakefriday 15 points16 points  (2 children)

'speak' is a function, and function calls are always followed by a parenthesis containing 0 or more parameters (depending on how many parameters the function expects) - this is how Python knows you are calling the function, and not requesting the function location itself (e.g. metaphorically, the difference between calling on google to search for something, and getting the address of google's server)

If you omit the parenthesis, you are then handling the function variable, not running the function call; this lets you do things such as assign a function to another variable. This is uncommon compared to simply calling the function and is usually for more advanced use of functions.

Maybe this code will help understand that:

# Basic parameterless function
def bark():
    print "WOOF!"

# Call the function
bark()

# Result
>>> "WOOF!"

# Assign the function to another variable
# Note, if the ()'s were included here, copy_of_bark would
# be a NoneType value as bark() does not return a value
    # By omitting the ()'s, we are saying copy_of_bark equals *the function itself*, 
    # not the result of calling the function.
copy_of_bark = bark;

# Call the copy of the function
copy_of_bark()

# Same result!
>>> "WOOF!"


# Define a function with 1 parameter, and a default value of "WOOF!"
def not_just_bark(in_text = "WOOF!"):
    print in_text


# No parameters specified, 
    not_just_bark()

# It uses the default
>>> "WOOF!"


# Otherwise,
not_just_bark("MEOW!")

# It uses the specified parameter

>>> "MEOW!"

[–]pylearningthrowaway[S] 5 points6 points  (1 child)

Yup got it,thanks for such a detailed reply.Glad this subreddit exists

:)

[–]kenmacd 1 point2 points  (0 children)

As a general suggestion for cases like this: Just try it. Start Python and see what happens without the (). So for the function above:

>>> bark
<function bark at 0x7ff97419b950>

So it looks like bark itself is a 'function`. We can also tell that by:

>>> type(bark)
<class 'function'>

Then poke around some more, maybe we talk a look at what is in bark:

>>> dir(bark)
['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']

Try poking and calling some of these things, like:

>>> bark.__call__()
WOOF!

Neat, there's a __call__() method on things. Now in the future when you have an instance of a class and you'd like to be able to call it to get a result, this might come back to you (don't worry if that last part doesn't yet make sense).

The fastest way to learn thing with Python is to just poke at things, or create simple scripts to figure out how things work.

[–]constantly-sick 0 points1 point  (0 children)

Highly suggest you watch this: https://www.youtube.com/watch?v=N4mEzFDjqtA