Major Python flaws

The Python programming language is famous for its simplicity and conciseness. A simple and understandable syntax similar to pseudo-code, as well as strong dynamic typing contribute to the quick and painless training of beginners.


The language interpreter takes care of all the low-level work, freeing the programmer from the need for manual memory management. The practical impossibility of getting a segmentation fault, as well as a convenient exception system equipped with clear messages, allow you to quickly debug programs. Situations when their falls due to an error require deep debugging are quite rare.


Irreplaceable integers and security when working with the containers of the standard library make Python a good means of pre-prototyping ideas, and a large number of high-quality mathematical libraries determine the leadership of this language in the fields of machine learning, data analysis and scientific computing.


More sophisticated programmers appreciate this language for convenient tools for constructing delayed, or, as they say, lazy, pipelines. In Python, this functionality is implemented by iterators and so on. generators. The asynchronous programming library is also pretty good.


But not everything with this language is easy and simple. In addition to a specific architectural solution called GIL , embodied in the main interpreter of the language CPythonand the associated problems with multithreading efficiency, Python also has more fundamental drawbacks that greatly limit its scope.


Speed


The main among these is considered to be its slowness, although this is parried with a certain degree of justice by the fact that the scripting language does not really need speed. For tasks requiring high performance, it acts only as a wrapper for manipulating the API of low-level libraries written in languages ​​that support AOT compilation . The most popular of these languages ​​at the moment are, of course, C and C ++. The first, for example, implements a widely used library NumPycreated for mathematical operations with arrays of arbitrary dimension. On the second - a growing framework for training neural networks PyTorch.


, - . , , , Cython, , , .


, , . :


  • Python . Bash, Sh , . . : Linux.
  • . : sqlite3, Django ..
  • , , . Python API . : NumPy, CuPy, PyTorch ..

Python


:


  • .
  • .

CPython, :


typedef struct _object {
    Py_ssize_t ob_refcnt;
    PyTypeObject *ob_type;
} PyObject;

ob_refcnt , . , , , , . ob_type . , , , , , .


, . , .


Python, -, . , .


. , [100, 200, 300, 400, 500], , , , int, , . , , , . , , .



, , Python, . , , , Java , , , . , Java, β€” , JIT-?


(PyPy ..) Numba, , . , , Python β€” :


  • -,


    , , , . .


    - :


    >>> x = 3
    >>> x = '36'
    >>> 
    >>> def foo(a, b):
    >>>     if b:
    >>>         return [a]
    >>>     return a
    >>> 
    >>> foo(x, True)
    ['36']
    
    >>> foo(x, False)
    '36'
    
    >>> class Bar:
    >>>     __slots__ = ('baz',)
    >>> 
    >>> x = Bar()
    >>> x.baz = 332
    >>> x.baz
    332
    
    >>> x.baz = 'Some string'
    >>> x.baz
    'Some string'
    
    >>> foo(x, True)
    [<__main__.Bar at 0x10ff0d700>]

    , . , , , , β€” , .


  • β€” .


    , , . , :


    >>> a = 5
    >>> b = True
    >>> 
    >>> if b:
    >>>     x = 100
    >>> x
    100
    
    >>> for i in range(10):
    >>>     for j in range(5):
    >>>         pass
    >>> print(i, j)
    9 4

  • . :


    >>> class Meta(type):
    >>>     def __new__(cls, name, bases, attrs):
    >>>         if list in bases:
    >>>             return super().__new__(cls, name, (tuple,), attrs)
    >>>         return super().__new__(cls, name, (list,), attrs)
    >>> 
    >>> class Foo(list, metaclass=Meta):
    >>>     pass
    >>> 
    >>> class Bar(tuple, metaclass=Meta):
    >>>     pass
    >>> 
    >>> issubclass(Foo, list)
    False
    >>> issubclass(Foo, tuple)
    True
    >>> issubclass(Bar, tuple)
    False
    >>> issubclass(Bar, list)
    True

    . , , , . , , , β€” , . .


    , , , .


    Foo Bar Meta, , . , list, , , tuple, β€”  list.


    .


  • , .


    >>> from collections.abc import Iterable
    >>> 
    >>> def wrap_class(cls):
    >>>     if issubclass(cls, Iterable):
    >>>         class FooCounter(cls):
    >>>             def count_foo(self):
    >>>                 return sum(1 for item in self if item == 'foo')
    >>> 
    >>>         return FooCounter
    >>>     raise TypeError(f'Class {cls} is not an iterable type')
    >>> 
    >>> wrap_class(list)([2, 3, 'foo', 'bar', 'baz', 'foo']).count_foo()
    2


Python. , MyPy, . , , . , , , typing , . , wrap_class -, .


, , Python 3.8, , , , , - .


, , , . , , Python Numba.



Python, : . , .


, . , , Julia. CPython β€”  .


Be that as it may, Python is a great tool for writing code fast. Its wide dynamic capabilities, as well as its comprehensibility and, I am not afraid of this word, the beauty of the syntax are determined by the fact that the process of compiling programs on it really causes satisfaction. Nevertheless, like any tool, this language has its drawbacks and it is useful to remember them before starting your projects.


All Articles