Python or not Python

I’ll talk about the problems I encountered when choosing Python3 as the first language.

I did not study programming at the university.

I do not want to start holywar.



In 2016, Google advised learning Python3 if needed:



1. Easy to start language.

2. Language for machine learning.

3. Language for simple 2d games.



1. I watched the first lecture of the Harvard CS50 course on the JavaRush website and realized that I didn’t want to:



#include <stdio.h> int main(int argc, const char *argv[]) { printf("Hello world\n"); return 0; }
      
      





When you can do this:



 print('Monty Python')
      
      





JavaScript scared a combination of three systems: JS / HTML / CSS.



2. Machine learning inspired all articles and examples to resemble cyberpunk and science fiction.



3. Can I program the games? Super!



I did import this, appreciated the philosophy of Python, and began reading Michael Dawson's book, Programming Python. Three weeks after “Hello World!” I wrote a clone of Life's game with a GUI interface. I fell in love with Python and when the difficulties started, I thought that I didn’t know enough and write bad algorithms.



Speed



I used tkinter and drew rectangles to create cells.

Field 800x640. The cell size is 10x10.



 class Square(object): def __init__(self, canvas, x, y, size, main): self.canvas = canvas self.cel = self.canvas.create_rectangle(x, y, x + size, y + size, fill='#EEEEEE') self.main = main self.color_change = True self.canvas.tag_bind(self.cel, '<ButtonPress-1>', self.paint)
      
      









Such a small field did not have time to completely fill up. About 600 cells were updated quickly, but as soon as I reduced the cell size to 4, the rate of change in the state of the cells and the response of the interface dropped.



I removed the Square class and started drawing pixels from the image. The interface worked fine, but after 200 cells everything was updated very slowly. I found a problem in the algorithm. Draw a cell of 4 pixels slower than 1 pixel.



 def fill_black(self, x, y, color): for i in range((-self.cell // 2) - 1, (self.cell // 2) - 1): for j in range((-self.cell // 2) - 1, (self.cell // 2) - 1): xx = (x + i) % self.can_width yy = (y + j) % self.can_height self.cell_matrix.put(color, (xx, yy))
      
      





After corrections, I got 600 cells at normal speed and very small figures on the screen. I managed to bring the speed to 900 cells when I stopped checking the image for the “black pixel” and began to store the state of the system in the matrix.



 def fill_black(self, coords, color, virt_color): x, y = coords self.virt_mat[x][y] = virt_color self.cell_matrix.put(color, (x, y))
      
      





I installed pygame, migrated the algorithm and got 9000 cells at 4 fps. But games run faster, and there are not only dots on the screen.

I began to suspect that it was a matter of language.

I tried Cython. A system of 30,000 cells earned at 4 fps.







I didn’t want C, but I got C.



How to pack for portability to another computer?



I dealt with this using py2app, but still did not understand how it works.



Standard library and third-party modules



I did not manage to evaluate this. I constantly installed something else and, sometimes, the modules did not want to work with each other. I had three venvs for separate application packaging with tkinter, tkinter + pillow and pygame. I looked in the site-packages folder and did not know what it depends on and why it lies there. I did not write my modules. I was looking ready.



In general, I went through this and wanted simplicity.

A language that can be seen.



I chose Lua. For some reason, I chose another language with a not quite familiar syntax :-)



Speed



I made Life on Lua + Love2d. I just used the language and engine functions.



 function Life:draw() love.graphics.setCanvas(self.canvas) love.graphics.clear() for y=1,self.rows do for x=1,self.cols do if self.cells[y][x]==1 then love.graphics.setColor(self.color) love.graphics.points(x-1,y-1) end end end love.graphics.setCanvas() love.graphics.setColor(set.WHITE) love.graphics.draw(self.canvas,self.x,self.y) end
      
      





With 111735 living cells, the system runs at 18 fps.







How to pack for portability to another computer?



I wrote scripts on Lua + Love2d to package applications for MacOS and Windows. Yes, there is an instruction on the site , but now for me this process is not as "magic" as with py2app.



Small standard library



I missed the usual Python functions: map, filter, reduce, range and had to make a functional library. There are no classes in Lua, but I read how to add them to the language and better understood how classes work in Python. Only with Lua did I like closures and generators.



When I tried all the functions of the standard library and a third of the functions from the modules for interacting with the C language, I had a general idea of ​​the language. This is a very pleasant feeling. Lua seems to be a good place to start, but the language is not popular.



If I could re-select the first language, then I would start with JavaScript.

Why?



1. The language runs in browsers.

2. Popular.

3. Getting to know the web.

4. JavaScript + pixi.js and the game Life will work without problems.



And Python, I would study along with machine learning algorithms.



All Articles