Python creates instance of None at the beginning - python

Python 2.7.12 (default, Nov 12 2018, 14:36:49)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> id(None)
9392928
I see that None in python has been already instantiated at the beginning without me doing anything. Can someone help me to understand why I cannot do this even if None is just a 'name' for an object.
>>> None = 3
File "<stdin>", line 1
SyntaxError: cannot assign to None

It's one of the many built in constants. See here - https://docs.python.org/2/library/constants.html

Related

bytearray to string python2 to 3

I need to get bytearray as string on python3.
on python 2.7 , str(bytearray) results the contents of bytearray in string format.
Python 2.7.18 (default, Feb 8 2022, 09:11:29)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> c = bytearray(b'\x80\x04\x95h\x00\x00')
>>> str(c)
'\x80\x04\x95h\x00\x00'
>>>
on python 3.6, even the "bytearray" keyword is added into the resulting string.
Python 3.6.8 (default, Aug 12 2021, 07:06:15)
[GCC 8.4.1 20200928 (Red Hat 8.4.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> c = bytearray(b'\x80\x04\x95h\x00\x00')
>>> str(c)
"bytearray(b'\\x80\\x04\\x95h\\x00\\x00')"
>>>
why is it happening so on 3.6 ?
how to get the exact same behavior on 3.6 as that of 2.7 ?
Note: I cannot do c.decode(), as those are compressed/pickled data which will result in invalid start byte errors.
Any suggestions please.
The __str__ method for the bytearray class is different in Python 3, to obtain a similar result you could try below snippet.
>>> str(bytes(c))
"b'\\x80\\x04\\x95h\\x00\\x00'"

disable python greeting/version info

When I start a python interactive session from the command line I am greeted by :
Python 3.9.6 (default, Jun 30 2021, 10:22:16)
[GCC 11.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
Is there a way of disabling that message so that I go immediately to the >>> prompt?
Yes, there is a way to do so.
Type in the cmd:
python -q
instead of
python
and this should do the trick.

How to check if a PosixPath is a directory or a file

With Path I can run path.is_dir(), how can I do the same thing with PosixPath?
How can I do the same thing with PosixPath?
Exactly the same way. PosixPath extends Path so has all the underlying methods:
Python 3.9.0 (default, Oct 12 2020, 02:44:01)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pathlib, os
>>> pathlib.PosixPath("/").is_dir()
True
>>> os.system("touch /tmp/aFile.txt")
>>> pathlib.PosixPath("/tmp/aFile.txt").is_dir()
False
>>> pathlib.PosixPath("/tmp/aFile.txt").is_file()
True

Once again, UnicodeEncodeError (ascii codec can't encode)

I'm running python 3.6 + gunicorn + django 2.0.5 in docker container with some cyrillic project and that's what I see when I try to log cyrillic strings in console with Django.
'ascii' codec can't encode character '\u0410' in position 0: ordinal not in range(128)
Also this what happens in shell
Python 3.6.5 (default, May 3 2018, 10:08:28)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> :�ириллица
The same time, when i'm running python 3.5 outside docker container, everything is ok:
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> Кириллица
Any ideas how to make python 3.6 inside docker work ok with cyrillic strings?
Use # -*- coding: utf-8 -*- in the first line of your python code.
And in your Dockerfile add:
ENV PYTHONIOENCODING=utf-8

How to surpress python's start-up information?

When I type "python" and return in shell, the following lines will come out:
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
How to surpress these lines please?
An easy way is to call Python as python -i -c "". This will also disable any start-up scripts, though. If you have a start-up script, you can also use python -i ~/.pythonrc.py (or however that script is named).

Categories

Resources