Python for beginners or not? [was Re: syntax difference]

Bart bc at freeuk.com
Sun Jun 24 15:03:13 EDT 2018


On 24/06/2018 19:36, Abdur-Rahmaan Janhangeer wrote:
> see for example
> 
> https://en.m.wikipedia.org/wiki/Bresenham%27s_line_algorithm
> 
> see the pseudocode, i was implementing some raster algos when i found
> myself aux anges
> 
> so close to py. i guess it was written in prehistoric times with the author
> trying to simplify stuffs

I'm sorry, but that kind of code is a mish-mash of old 
Algol/Pascal-style languages. I doubt it was meant to be Python.

Python doesn't have a monopoly on clear syntax.

This is the first example from that link (I've taken out comments):

  function line(x0, y0, x1, y1)
      real deltax := x1 - x0
      real deltay := y1 - y0
      real deltaerr := abs(deltay / deltax)
      real error := 0.0
      int y := y0
      for x from x0 to x1
          plot(x,y)
          error := error + deltaerr
          while error ≥ 0.5 then
              y := y + sign(deltay) * 1
              error := error - 1.0

This has some problems: it's using 'function' when it doesn't return a 
value (those languages used 'proc' or 'procedure' in that case). It 
doesn't give a type for the parameters, and it uses while/then rather 
than the usual while/do.

So it's rather sloppy even for pseudo code. The only similarity with 
Python is the lack of block delimiters, but then with later examples 
they /are/ used, for if-else-endif.

Below, the first block is that code tweaked to a static language of 
mine. The second is the same code tweaked to Python.

It was less work to adapt it my syntax rather than Python. All versions 
however use a decidedly un-Pythonic style, which means the difference 
between the two below isn't that great, even though they are different 
languages. But imagine trying to adapt Pythonic code to work in the 
other language; it would be much harder (apart from one being dynamic 
and the other not).

---------------------------------
  proc line(int x0, y0, x1, y1)=
      real deltax := x1 - x0
      real deltay := y1 - y0
      real deltaerr := abs(deltay / deltax)
      real error := 0.0
      int y := y0
      int x
      for x := x0 to x1 do
          plot(x,y)
          error := error + deltaerr
          while error = 0.5 do
              y := y + sign(deltay) * 1
              error := error - 1.0
          end
      end
  end
----------------------------------
def line(x0, y0, x1, y1):
      deltax = x1 - x0
      deltay = y1 - y0
      deltaerr = abs(deltay / deltax)
      error = 0.0
      y = y0
      for x in range(x0,x1+1):
          plot(x,y)
          error = error + deltaerr
          while error == 0.5:
              y = y + sign(deltay) * 1
              error = error - 1.0


-- 
bart



More information about the Python-list mailing list