-2

Consider the following Python 3.7.2 code [*] i ran in IDLE (I have added line numbers for reference):

[1] >>>> a_tuple = (1,2,3)
[2] >>>> a_tuple
[3] (1,2,3)
[4] >>>> print(a_tuple)
[5] (1,2,3)
[6] >>>> an_ndarray = numpy.array([1,2,3])
[7] >>>> an_ndarray
[8] array([1, 2, 3])
[9] >>>> print(an_ndarray)
[10] [1, 2, 3]

I am learning computer science terminology and Python, and I have the following questions/requests:

  1. What am I doing in [2], calling the object? Or is calling reserved for functions?
  2. What is being returned in [8]? It is not the contents of the ndarray. It doesn't make sense, but it is as if what is returned is the function call to create it, except it lacks the numpy. part.
  3. Is [1,2,3] in [6] considered a list or just the syntax for numpy.array() arguments?
  4. Is there a way to obtain the contents of an ndarray without the array() part in [8] and without using print()?
  5. Using precise technical terms, could you elaborate as to why [3] and [8] are so different?
  6. In general, what is what one types in the console called (e.g. [2])? Commands, calls, inputs?
  7. What are [3], [5], [8] and [10] called? Output? What are some synonyms?
  8. What is [*] (see the first sentence) called? It is not precisely code.
Zweifler
  • 375
  • 1
  • 4
  • 12
  • Printing an object calls `__str__` special method, and calling it (which is not a good description. Calling requires parenthesis like object()) calls `__repr__` special method. – MSH Oct 15 '21 at 21:19
  • 3
    These sounds an awful lot like homework questions – Adrien Levert Oct 15 '21 at 21:20
  • (1) No, calling always requires appending parentheses, often with arguments in it. (2) It's `repr(an_ndarray)`. The Python shell always asks for the representation of the evaluated expression. Classes can define how their objects should be represented. – Michael Butscher Oct 15 '21 at 21:23
  • @AdrienLevert Well, they are not. – Zweifler Oct 15 '21 at 21:23
  • @MSH that explanation is only (somewhat) correct when using a REPL. Anywhere else, evaluating an object does not call `__repr__` – DeepSpace Oct 15 '21 at 21:23
  • 1. You don't call the variable there. It is a feature of the interpreter to print the result of a line when in interactive mode. Since the line contains the variable it is printed. – Klaus D. Oct 15 '21 at 21:23
  • And BTW: only one question per question is allowed. – Klaus D. Oct 15 '21 at 21:25
  • @DeepSpace thank you for correction. I think I need learn more about these facts. – MSH Oct 15 '21 at 21:26
  • @KlausD. Thanks for the heads up; still learning how to ask. – Zweifler Oct 15 '21 at 21:26
  • 1
    Welcome to Stack Overflow. This question seems to *"Need more focus"*, please consider the guidelines [*"Why are some questions closed??"*](https://stackoverflow.com/help/on-topic) – bad_coder Oct 16 '21 at 23:11

2 Answers2

3

First, a lone variable in the REPL will print its object representation, by calling the repr() function on that object, not "calling the variable"; that would look like a_tuple()

Assignment obviously has no output

This explains 1-3, and 6-8

A print statement will run str() function on the object and show that output.

Both are mapped to "magic functions" defined on all object types

What is the difference between __str__ and __repr__?

rule of thumb: __repr__ is for developers, __str__ is for customers

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
1

IDLE is an example of a Read-Eval-Print Loop, AKA REPL. You type a statement, it executes it, and if it's an expression with a value it prints the value.

  1. You call a function, you use or assign a variable. So in [2] you used the variable a_tuple. IDLE evaluated it and printed its value.
  2. In [8] it's printing the representation of ndarray, which is a numpy array. The representation of numpy arrays is shown as the contents in square brackets inside array().
  3. [1, 2, 3] is a list, being passed as the argument to the numpy.array() function. numpy will then create an array whose contents are the elements of the list.
  4. You can convert the array to a list with ndarray.tolist().
  5. The only difference between [3] and [8] is the type of value that you're evaluating and printing. [3] is a list, [8] is an array. Different types are shown in different ways.
  6. They're Python statements.
  7. Yes, they're output. More specifically, they're the values of the expressions that you typed on the previous lines.
  8. Yes, it's code. The purpose of an REPL is to allow you to execute code interactively, as opposed to running code in a script file.
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks! So, for 4., there is no way of simply obtaining the contents without directly indexing them or calling other functions? I guess the answer to 5. is that [2] and [8] are representations of objects; I didn't know this concept. – Zweifler Oct 15 '21 at 21:54
  • Did you mean to compare `[3]` and `[8]`? – Barmar Oct 15 '21 at 21:56
  • True; just edited it. – Zweifler Oct 15 '21 at 21:58
  • Everything that's printed is the result of calling some function that formats the value as a string. When IDLE prints an object, it prints its "representation", which tends to be more detailed or technical (strings are shown with quotes and escape sequences, for example). The `print()` function prints its "string", which is more human-oriented (strings are shown without quotes, escape sequences are interpreted). – Barmar Oct 15 '21 at 21:59