Calls in Python (Calls in Python), lesson, page 724625
https://www.purl.org/stefan_ram/pub/calls_python (permalink) is the canonical URI of this page.
Stefan Ram
Python Course

Calls in Python 

Syntax

Syntax (simplified)

expression

.------------. .-. .-.
--->| expression |--->( ( )--->( ) )--->
'------------' '-' '-'

Semantics

When the call expression is evaluated, the initial subexpression is evaluated.

The ₍rɪˈzʌlt₎ result of this evaluation is an object, which then is called  (i.e., activated), which is why it is calls the callee.

The callee then is free to control the computer. – What it does here is the action  of the call which causes the effect  of the call.

The callee then can cause the evaluation to end, by specifying the value the evaluation should yield. – This is the value  of the call. One also says that this value is the result  of the function or was returned  from the function

After it has done this, the evaluation terminates with the value that was specified by the callee. The callee has returned the control of the computer.

Explanation

What exactly will happen when an object is called, depends on that object.

Some objects are not callable, which means that they cannot be a callee. Calling such objects will cause the program to abort with an error message.

A callable object  will also be called a callable.

Example ›dir

Console transcript
dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']

Calling ›dir‹ yields a list. The list is shown above. It is enclosed in brackets and its individual items are separated by commas. Each entry above has the type string.

The result of »dir()« is a list with the names of the module the call expression is contained in. (The result may not always look the same as above.)

Console transcript
dir
<built-in function dir>

dir‹ is a function that was written in C  (hence »built-in«).

?   Pronunciation

How would you read the following text to someone on the phone (if it would appear in a longer Python  program that you were reading aloud)?

Expression
dir

?   Pronunciation

How would you read the following text to someone on the phone (if it would appear in a longer Python  program that you were reading aloud)?

Expression
dir()

Example ›str

Console transcript
str()
''

The class ›str‹ is callable, but calling it just yields the empty string, which has no discernible use for us.

Console transcript
str
<class 'str'>

str‹ is a class.

Example ›1

Console transcript
1()
TypeError: 'int' object is not callable

The object ›1‹ is not callable, which is exactly what the error message is saying! (Actually, the message intends to say, “An  object of type int, such as ›1‹, is not callable.”)

Example ›vars

Console transcript
vars()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>}

Calling ›vars‹ yields a dictionary. The dictionary is shown above. It is enclosed in brackets and its individual items are separated by commas. Each item consists of a key  (in front of the colon »:«) and a value  (after the colon »:«).

The result of »vars()« is a dictionary with the names of the module the call expression is contained in as keys and their respective values. (The result may not always look the same as above.)

Console transcript
__name__
'__main__'
Console transcript
__loader__
<class '_frozen_importlib.BuiltinImporter'>
Console transcript
__annotations__
{}
Console transcript
vars
<built-in function vars>

vars‹ is a function that was written in C.

Example ›input

Console transcript
input()
A
'A'

When ›input‹ is called, the program waits for a user input and the returns this as a str object. Above, the first line is the call expression, the second line the user input, and the third line the value returned from the call.

When ›input‹ is running, you need to enter a text and then press the Enter key so that the program can continue. Or, press Ctrl-C to abort the program.

Example ›random

random‹ yields a random number between 0 (inclusive) and 1 (exclusive).

Console transcript

from random import random

random()

0.206133452324827

The probabilities for each of the possible values are approximately equal.

Call expressions as operands

A call can be an operand.

Console transcript

from random import random

2 * random()

1.9265340087149025

?   Ranges

In what range is the value of the expression »random() + 10«?

In what range is the value of the expression »10 * random()«?

In what range is the value of the expression »10 * random() + 10«?

In what range is the digit before the decimal point of value of the expression »3 * random()«?

List expressions as operands

Lists can be added and multiplied just like str objects.

Console transcript
"abc" + "abc"
'abcabc'
dir() + dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', '__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']
2 * "abc"
'abcabc'
2 * dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', '__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']

Input calls as operands

The following program echoes the user input two times.

Console transcript
2 * input()
D
'DD'

The following program appends a »d« to the input.

Console transcript
input() + "d"
an
'and'

The following program prepends a »t« to the input.

Console transcript
't' + input()
he
'the'

The following program concatenates the two inputs.

Console transcript
input() + input()

A

B

'AB'

/   Parenthesizing a string

Write a program that reads a string and then prints the parenthesized string.

Console transcript
input
'(input)'
Console transcript
test
'(test)'

/   Trebling a string

Write a program that reads a string and then prints the string three times.

Console transcript
input
'inputinputinput'
Console transcript
test
'testtesttest'

/   Trebling and then parenthesizing a string

Write a program that reads a string and then prints a pair of parentheses containing the triple concatenation of the string.

Console transcript
abc
'(abcabcabc)'

/   Parenthesizing and then trebling a string

Write a program that reads a string and then prints the triple concatenation of the parenthesized string.

Console transcript
abc
'(abc)(abc)(abc)'

Lambda expressions

Syntax

Syntax (simplified)
expression
.------. .-. .------------.
--->( lambda )--->( : )--->| expression |--->
'------' '-' '------------'

Semantics

A lambda expression designates a function that returns the value specified by the expression after the colon »:«.

Example

»lambda:2« designates a function that returns the value «2».

Console transcript
lambda:2
<function <lambda> at 0x000000000…>
_()
2

The subexpression becomes part of the function and is only evaluated when the function is called.

Console transcript

from random import random

lambda:random()

<function <lambda> at 0x000000000…>
_()
0.202895722089483

In a sense, a »lambda« is the opposite of a call.

Console transcript
( lambda:9 )()
9
( lambda:lambda:9 )()()
9

/   Exercise

Write a lambda expression for a function the returns the string «"abc"» (that is, three letters) when called.

 

About this page, Impressum  |   Form for messages to the publisher regarding this page  |   "ram@zedat.fu-berlin.de" (without the quotation marks) is the email address of Stefan Ram.   |   A link to the start page of Stefan Ram appears at the top of this page behind the text "Stefan Ram".)  |   Copyright 1998-2020 Stefan Ram, Berlin. All rights reserved. This page is a publication by Stefan Ram. relevant keywords describing this page: Stefan Ram Berlin slrprd slrprd stefanramberlin spellched stefanram724625 stefan_ram:724625 Calls in Python Stefan Ram, Berlin, and, or, near, uni, online, slrprd, slrprdqxx, slrprddoc, slrprd724625, slrprddef724625, PbclevtugFgrsnaEnz Explanation, description, info, information, note,

Copyright 1998-2020 Stefan Ram, Berlin. All rights reserved. This page is a publication by Stefan Ram.
https://www.purl.org/stefan_ram/pub/calls_python