r/Python • • Nov 14 '17

Senior Python Programmers, what tricks do you want to impart to us young guns?

Like basic looping, performance improvement, etc.

1.3k Upvotes

629 comments sorted by

View all comments

27

u/RaionTategami Nov 14 '17 edited Nov 14 '17

Nothing better than getting in to the habit of writing unit tests to improve the quality of your code. It actually causes you to write better code not just less buggy code because testable code is more modular.

Also stop writing classes! Edit: I don't really think you should never write classes but I see a lot of them in code when they aren't really needed.

0

u/[deleted] Nov 14 '17

[deleted]

11

u/snipav1 Nov 14 '17

I would have to second this statement. I'm a systems engineer at a very top tech company and even when we write our scripts we use classes to organize our code a lot better. I just can't imagine not using classes.

25

u/RaionTategami Nov 14 '17

Classes in Python are not for namespaces! That's what modules are for.

2

u/KODeKarnage Nov 14 '17

Classes are for what I use them for. That's pythonic.

2

u/tilkau Nov 15 '17

They're namespaces that you want to attach to a blob of data because the members are tightly related to that kind of data.

So It might be better to say: if you could do it with a module and there wouldn't really be any functional difference, do it with a module.

3

u/RaionTategami Nov 15 '17

I see what you are saying. Classes are to organize data, not code, is that fair?

→ More replies (1)

5

u/ben174 Nov 14 '17

https://youtu.be/o9pEzgHorH0

This video explains it quite well.

5

u/snipav1 Nov 14 '17

I've seen this video before. This advice is very subjective. I like how all the top comments on this video are all related to making sure you don't stop writing classes lol. Object Oriented is not meant to write less code, but a maintainable one. One can replace 5 classes and 500 LOC with 5 lines compressed in a python function, yes. But that will be completely unreadable. Examples presented in the video are extreme and do not help to support the topic, which has got sense in some cases.

2

u/zebraJoe Nov 14 '17 edited Nov 14 '17

They might be referring this video it goes into detail when to not uses classes and shows some good examples of bad use of classes. Also it's not to technical or long.

https://youtu.be/o9pEzgHorH0

→ More replies (1)

7

u/ICanAdmitIWasWrong Nov 14 '17

I can't imagine programming in Python without classes.

This alone should be a warning sign. Writing python without classes (or with minimal classes) is very easy and natural. Try it some time. A lot of things are much, much easier without an object dogma straitjacket.

13

u/TankorSmash Nov 14 '17

FWIW this isn't as helpful as you probably intended. Maybe you could provide some reasons why it's easy and natural. Sorta sounds like you're ragging him for not knowing already.

→ More replies (5)

3

u/nicwolff Nov 14 '17

Of course you should write classes if they fit the problem you're solving, but don't write unnecessary classes.

Let's start with: don't write singletons. If you want an object available across your project that encapsulates a single set of attributes and behaviors, that's what a module is.

3

u/RaionTategami Nov 14 '17

OOP in my opinion is just bad. But that's my opinion. Never writing classes is obviously going overboard so I'm not really saying you should never do it. A good rule of thumb is only writing a class if you have a new data type. If you want to bundle data then things like dicts and tuples (esp named tuples) are better. I see people using them when they are not needed and their code would be much cleaner without them.

Here's a recent example a saw from a neural network lib. They wanted to create layers which you could then call;

class Layer(object):

    def __init__(self, params):
       self.params = params

    def __call__(self, x):
        # do something with x and self.params

People seem to love this pattern but this is maddening to see because this is a closure.

def layer(params):
    def f(x):
        # do something with x and params
    return f

Not only is that shorter its less prone to bugs.

So what I should really say is try not to write classes, there's probably a better way.

19

u/[deleted] Nov 14 '17

God no. You can't test that closure. You just made that whole function a black box.

→ More replies (2)
→ More replies (10)
→ More replies (2)

22

u/[deleted] Nov 14 '17

This is kind of obnoxious advice. Unless you really do believe no one should ever use classes in Python, then it's just terrible advice.

Assuming it's the former, care to elaborate on when/why you think newer coders are abusing classes, so that you aren't just throwing this nugget out there for a bunch of people to believe that the simple act of building an object with a class is incorrect?

Assuming it's the latter what's your alternative? Dictionaries or named tuples for every single object? That'll be fun to read, and manage.

8

u/RaionTategami Nov 14 '17

Yes you are right I shouldn't have been so absolutist. I tried to explain myself better in the other reply

5

u/[deleted] Nov 14 '17

I saw, and upvoted the reply. Thanks for elaborating!

11

u/ubernostrum yes, you can have a pony Nov 14 '17

Assuming it's the latter what's your alternative? Dictionaries or named tuples for every single object?

Not the commenter you're replying to, but: people use way too many classes. Sometimes it's because they've come from Java. Sometimes it's because this is their first OO language or even their first programming language and they go overboard with the classes.

There are times to use classes, and there are times to use lighter-weight data structures or to just write functions. Going all-in on one or the other is a problem.

13

u/[deleted] Nov 14 '17

I agree. I'm looking for the OC to elaborate. I think it's really bad practice to come into an advice post and just throw such an extreme nugget in with no elaboration.

It's completely unhelpful. Even just saying there's a time to use it and a time to not isn't helpful. If you can't/won't elaborate to explain your advice in a useable way, then don't bother commenting. No one is forcing a user to give advice here, so if they're going to give it, it should be productive.

3

u/RaionTategami Nov 14 '17

Fair cop. Toned down the advice somewhat.

4

u/beecee808 Nov 14 '17

I'm interested in the "stop writing classes" comment. Could you elaborate on why? Is it just that we should be using the standard built in types, that people tend to not do it properly, or something else?

I'd consider myself a solidly "okay" Python programmer but sometimes fall into old habits from my knowledge of OOP in C++ or Java. Tips like these can go a long way for people like me.

2

u/RaionTategami Nov 14 '17

See my other replies.

7

u/muposat Nov 14 '17

Python without classes is ugly. Do you just shuttle data via global variables? Dozens of arguments?

→ More replies (9)
→ More replies (1)