DEV Community

Bailey Matthews
Bailey Matthews

Posted on • Updated on

My love hate relationship with Python

Originally posted on bailey.guru.

Python was the first language I learned back when I was 8 and I loved it. I could make a cool console based game or pretend I was hacking, who wouldn't want to do that? I have a few of my early creations I found lying around on my old computer hosted on my Github Gist page. And they honestly were super cool! I loved the fact I could type maybe 5-10 lines and still have something cool to show - it was simple. It didn't have any of the complicated structures of C# or other C languages.

But now I've changed and I've learnt languages such as JavaScript and Go and I just can't go back - it's too simple. I found that while programming my GCSE Project I loved using Python again but hated the lack of structure... where are my brackets?

If I could do something like below, then I would be in love with Python.

def function(text) {
  ... do something 
}
Enter fullscreen mode Exit fullscreen mode

Brackets

So, naturally to add a sense of self security and structure to my Python project, I decided to Google something along the lines of "How to get curly brackets in Python", to which I found an article on Stack Overflow titled "Is it true that I can't use curly braces in Python?". It seems whoever wrote that seemed a little upset. Nevertheless, I clicked on it and started reading. There were many sarcastic responses such as:

if foo: #{
    print "it's true"
#}
else: #{
    print "it's false!"
#}
Enter fullscreen mode Exit fullscreen mode

Which I did actually find quite funny but there was one the caught my eye, I saw an import function 👀. It said:
"You can try to add support for braces using a future import statement, but it's not yet supported, so you'll get a syntax error." and preceded to show this snippet:

>>> from __future__ import braces
  File "<stdin>", line 1
SyntaxError: not a chance
Enter fullscreen mode Exit fullscreen mode

If you look closely you can see the syntax error "not a chance". It turns out this is a little easter egg from the developers behind Python of essentially saying "Nope, we're never adding it!" which kind of makes me a little sad but then I suppose good on them for sticking to their simple design scheme.

Further Research

So, although I now knew brackets were never going to be a part of Python, I decided to see if anyone had made a compiler or pre-processor for this sort of thing. And to my amaze, they had! I found Bython - Python with braces. And from what the project says, it looks pretty easy to configure, you install and use a separate command instead of Python to run. And it comes with a command to convert Python to Bython. Pretty neat huh!

Although I will probably never use Python again (or at least for a while), it's nice to know that Bython is a thing. And Mathias sums this article up pretty well: Because Python is awesome, but whitespace is awful.

Top comments (9)

Collapse
 
mattconway1984 profile image
Matthew Conway

I was expecting to find something like "I hate managing conflicting package dependencies for large projects" or "I hate some aspects of duck-typing" etc... Then I read a bit and found a rant about braces, or a lack of! If that's something to "hate" it's a new one for me! Indentation is fine, setup your editor (for all languages to replace tab with 4x space, strip trailing whitespace and you're sorted).

I don't get what's so bad about

def foo():
    print("foo")

vs

def foo()
{
    print("foo")
}

The only thing (in regards to Python function syntax) which I would consider ranting about is abuse of lambda (considering lambda==annoymous function, why would anyone name one!), i.e.

foo = lambda x:print(f"value of x is {x}")
Collapse
 
thebouv profile image
Anthony Bouvier

You may want to learn how to distinguish between parentheses () and brackets {} first. Then you can complain about what is or is not lacking.

;) (that's a semi-colon and parenthesis to make a winky face in case you needed to know).

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
thebouv profile image
Anthony Bouvier

Ha! I'll take that on the chin, it was rather rude of me as written. I actually intended to a be a bit funnier but it got lost in my haste to respond.

At the end of the day I'm just pointing out that you say parentheses several times, but you're actually talking about missing curly braces. You probably want to edit that to make your case a bit stronger, yeah?

My own opinion on the matter: I use languages daily that are bracket heavy (JS, PHP) and those that are not (Python) and it works out fine. I actually really like how clean Python is without the braces -- to me, opposite of you, it is much easier to read the code because there is less noise.

If your code is so long and deeply nested that you easily get lost, you're already doing Python wrong anyway. Super long complex functions should be broken down into smaller, easier to digest ones. And then you wouldn't have such a hard time reading just a few indentation levels. Give it a try again. Python is pretty great, but there are also plenty of other great languages too.

Collapse
 
gilblinov profile image
Gil Blinov

Hey Bailey,

Can you please explain what bothers you about having to use indentation as a replacement?
Really interested.

Collapse
 
bailey profile image
Bailey Matthews

The main problem for me is the ability to easily see what’s happening - if you see a func var() {} in JavaScript you know that whatever is in the brackets is part of that function, and you can structure that function as much as you want, along as it is in the brackets.

With Python, one indent to many causes compiling issues and it’s hard to keep track. For example I was writing a class and obviously this class had functions, so, while writing these I made a whole function but wondered why it wasn’t able to be called - It was due to the fact that it was indented one too many and became part of the past function. Now this isn’t a problem for a small script. But when you have sub functions and other things suddenly you can be indenting 4-5 times, this causes a lot of distraction and frustration when trying to figure out what belongs to what.

All in all I just love the ability to write

func var() {
    ... stuff to do

    few comments

    ... little more stuff
}

func foo() {
    ... stuff to do

    few comments

    ... little more stuff
}

instead of

def var():
    .. stuff to do 

    few comments 

  ... more stuff

def foo():
    .. stuff to do 

    ... wait what function is this?

I hope this explains it clearly :)

Collapse
 
gilblinov profile image
Gil Blinov

Yeah.

It's a matter of preference.
I had the (dis)pleasure of refactoring code that with not proper indentation, it colours my perspective.

It's a good thing choices exists.
Just be mindful of personal bias

Collapse
 
bailey profile image
Bailey Matthews

And while proof reading this, notice one of my Python functions wouldn’t run due to an indent error?

Easy to miss...

Thread Thread
 
gilblinov profile image
Gil Blinov

So is an extra / missing parenthesis pair ;)