Better programming in the python ecosystem.

1 A better python

I am not entirely happy with python, but there is so many benefits in using python, if only there was a way to use python with all the benefits but not having to deal with some of the downsides.


cuddles_transparent_small.png

credit: hy-lang

1.1 Introducing Hy

Oh wait but there is, it is called hy. Now if you are not a "crazy lisp user" you might not be all that hot on the idea of writing parentheses around all your code all the time, but there is a few benefits to this, also it is not more parens really they are just placed differently.

(print "Hello, World!")

Only the parens are different between these two.

print("Hello, World!")

it is the same characters, just the paren changed place in hy, now this is a simple example. There are some benefits from this one is the fact that more functional style code is possible in hy, which is not possible in python directly.

1.2 Differences

So there is a few small differences that you need to know.

1.2.1 Tuples

  1. Python
    tup = ("a", 2)
    
  2. Hy

    The way to do this in hy is a bit different.

    (setv tup (, "a" 2))
    

    This will the same thing in hy as the other code do in python. The (, ) is the construct for making a tuple in hy. There is only that one comma, the rest of the list should not contain any.

1.2.2 List

  1. Python

    The list in python is very simple

    l = [1,2,3,4]
    
  2. Hy

    But in hy you can do it much better

    (setv l [1 2 3 4])
    

1.2.3 Set

  1. Python
    s = {1,2,3}
    

    Simple and hy has a the same idea again.

  2. hy
    (setv s {1 2 3})
    

    again no commas.

1.2.4 Dict

  1. Python
    d = {"a": 1, "b": 2}
    

    See this is just way to much typing

  2. Hy

    Hy has a much nicer way to write this, and it just generates the python code anyway.

    (setv d {"a" 1 "b" 2})
    

    So you don't need anything but a values.

1.3 Hy adds stuff to python

So since hy is a lisp that runs on python there are things in hy that python does not have by itself.

1.3.1 macro

This can be very useful if you want to do some heavy compilation, but ideally if you could just do it at compile time and then never again that would be very nice, this is exactly what macros do, oh and allow you to crazy things like adding new language features, well to some extent at least.

The only thing you should be aware about with macros is that if you do not have a value at compile time it cannot be a macro.

(defmacro m []
  (setv x (** 2 20))
  x)

If you run this twice, it will take some time once, but then the second time it is compiled into the program and returns much much faster.

If this has whetted your appettite for a better way to write python there is additional documentation https://docs.hylang.org/en/stable/tutorial.html

Or if you think I am crazy for suggestion this you can always tell me in the comments, however if this is in any way useful, I appreciate votes.

H2
H3
H4
3 columns
2 columns
1 column
2 Comments
Ecency