Previously I have been working with RStudio and I'm used to R showing the results of all commands without additionally giving the "print" command. For instance, if I type x=3 x
I will receive in the console "[1] 3", i.e. the output of the code. However, if I try to do something similar with Spyder, for instance, array_np = np.random.normal(0,1,(5,3)) array_np
I will receive basically nothing as an answer, thus I will instead have to put print(array_np)
command. So is it possible to somehow enable output of the code in Spyder without "print" command?

- 55
- 4
1 Answers
Python print rules
There is a way to bootstrap this behaviour onto an object in Python
Custom get/set behaviour on a value
from typing import Any
"""
typing is possibly outside of the scope of this answer,
but I'm assigning Any as part of the
initialisation of the member, you can also say _private_member = 0
if this causes version issues
"""
class CustomObject:
_private_member: Any # not really private, just using convention
@property
def public_member(self):
print(self._private_member) # print here if you want to print on access
return self._private_member
@public_member.setter
def public_member(self, a):
print(a) # print here if you want to print on assignment
self._private_member = a
thing = CustomObject()
thing.public_member = 56
Output:
>>> 56
thing.public_member # on access
Output:
>>> 56
(This is by no means the only way to achieve something like this. One could, for example, inherit from the sort of object one is getting values from (np.ndarray, for example) and then add a print statement to a relevant method somewhere.)
Behaviour of the python shell
>>> x = 5
>>> x
5
This is also found in jupyter notebooks (which use the IPython console that Spyder defaults to using)
Essentially, the last line of the shell script does get echoed to the terminal by default
But, ultimately, the default behaviour of the python interpreter when running a script (example.py, for example) is to print nothing in response to the following:
x = 5
x
There is some related discussion to the particular behaviour of IPython consoles here: Controlling output/pagination of iPython Console in Spyder? Equivalent to More (on/off) in Matlab?

- 51
- 3