Return value of 'print' function? - python

I'm python beginner.
I am wondering what the return value of the 'print' function is.
I tried type(print(3)) and didn't work.
I tried to find the api document but I could only find the pprint function.

print() returns None in Python3.
$ python3
Python 3.3.2+ (default, Feb 28 2014, 00:52:16)
[GCC 4.8.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> type(print(3))
3
<class 'NoneType'>
In Python2 print is a statement, so doesn't return anything...
$ python2
Python 2.7.5+ (default, Feb 27 2014, 19:37:08)
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> type(print(3))
File "<stdin>", line 1
type(print(3))
^
SyntaxError: invalid syntax
...unless you use from __future__ import print_function
$ python2
Python 2.7.5+ (default, Feb 27 2014, 19:37:08)
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import print_function
>>> type(print(3))
3
<type 'NoneType'>

Related

None difference for Python versions

I've read a lot about Python versions differences but never met those
Python 2.7.18 (default, Mar 8 2021, 13:02:45)
[GCC 9.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> None.__eq__("abc")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute '__eq__'
and
Python 3.8.10 (default, Mar 15 2022, 12:22:08)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> None.__eq__("abc")
NotImplemented
I found it already being in Python 3.2
Python 3.2.6 (default, Jan 18 2016, 19:21:14)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> None.__eq__("abc")
NotImplemented
What I wan't to know if it is told somewhere in docs or somewhere. Didn't find anything here. Any sources?

simple question: in python3 env, why can't run simple print?

print("hello", end="")
output:
[Running] python -u "/home/a/code/projects/project1.py"
File "/home/a/code/projects/project1.py", line 1
print("hello", end="")
^
SyntaxError: invalid syntax
settings.json
{
"python.linting.pylintEnabled": false,
"python.linting.enabled": true,
"python.linting.flake8Enabled": true,
"python.linting.pydocstyleEnabled": false,
"python.pythonPath": "/usr/bin/python3.6"
}
been looking up about why code is being run on older version of python in vscode... can't figure it out.
That's because you're using Python 2:
Python 2.7.16 (default, Oct 10 2019, 22:02:15)
[GCC 8.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello", end="")
File "<stdin>", line 1
print("hello", end="")
^
SyntaxError: invalid syntax
In Python3 it wouldn't happen:
Python 3.7.3 (default, Dec 20 2019, 18:57:59)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello", end="")
hello>>>

NixOS python package version pinning (E.g requests 1.0.0) in /etc/nixos/configuration.nix

I am currently playing around with nixos and python. I'm wondering how to pin python package versions in /etc/nixos/configuration.nix
What i tried is the following:
let my-python-packages = python-packages: with python-packages; [
pandas
"requests==1.0.0"
];
Expected result
[jane#nixos:~]$ python
Python 2.7.17 (default, Oct 19 2019, 18:58:51)
[GCC 8.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
>>> import requests
>>> requests.__version__
'1.0.0'
Actual result
Python 2.7.17 (default, Oct 19 2019, 18:58:51)
[GCC 8.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
>>> import requests
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named requests
After that i tried this in configuration.nix:
# and in the NixOS manual (accessible by running ‘nixos-help’).
{ config, pkgs, ... }:
let nixpkgs.config.packageOverrides = super: {
python = super.python.override {
packageOverrides = python-self: python-super: {
requests = python-super.requests.overrideAttrs
(oldAttrs: {
src = super.fetchPypi {
pname = "requests";
# sha256 hash of requests 1.0.0 as on pypi
sha256 = "f10d8fbcc02a58056ab44f79ff9b3f9fe78e410296527885250bbb36d15be8c6";
};
});
};
};
};
my-python-packages = python-packages: with python-packages;
[
pandas
requests
];
python-with-my-packages = pkgs.python2.withPackages my-
python-packages;
Actual result:
[jane#nixos:~]$ python
Python 2.7.17 (default, Oct 19 2019, 18:58:51)
[GCC 8.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
>>> import requests
>>> requests.__version__
'2.22.0' # this should be 1.0.0
So how can i pin python packages to specific versions in /etc/nixos/configuration.nix ?

string.maketrans unicode handling: pypy vs CPython

Pypy doesn't seem to handle string.maketrans() when arguments are unicode, however CPython does:
$ python
Python 2.7.5 (default, Oct 11 2013, 14:51:32)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import string
>>> string.maketrans(ur"-/[] ", ur"_____")
'\x00\x01\x02\x03\x04\x05\x06...'
$ pypy
Python 2.7.13 (c925e73810367cd960a32592dd7f728f436c125c, Jun 08 2017, 19:14:08)
[PyPy 5.8.0 with GCC 6.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>> import string
>>>> string.maketrans(ur"-/[] ", ur"_____")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".../pypy-5.8-linux_x86_64-portable/lib-python/2.7/string.py", line 78, in maketrans
buf[ord(fromstr[i])] = tostr[i]
TypeError: 'unicode' object cannot be interpreted as an index
Didn't find anything relevant on http://pypy.readthedocs.io/en/latest/cpython_differences.html.
Is this a bug of CPython or PyPy?
That's a "bug", i.e. an unexpected difference. Fixed in 7fe0041fccaa (see line 78 of https://bitbucket.org/pypy/pypy/raw/default/lib-python/2.7/string.py).

how to address Python module inspect not having function getcallargs

I am trying to use the module inspect in two different environments. In one of the environments, everything is fine. In the other, inspect appears to be missing the function getcallargs. I am not sure what's going wrong. I'm also not sure how to check the version of inspect that is being used in each environment. How can I get inspect to work in the problematic environment?
The environment that works fine is as follows:
user1#computer1:~>python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import inspect
>>> print inspect.getcallargs
<function getcallargs at 0x7ff122c0a578>
The environment that breaks is as follows:
(virtual_environment)-bash-4.1$ python
Python 2.6.6 (r266:84292, Jan 23 2014, 10:39:35)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import inspect
>>> print inspect.getcallargs
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'getcallargs'
In Python 2.6 the inspect module does not have the getcallargs function.
https://docs.python.org/release/2.6/library/inspect.html
Python 2.7 does have getcallargs
https://docs.python.org/2/library/inspect.html

Categories

Resources