OK, this may be a dumb question but I'll ask anyway. I am starting to see the benefits of using classes. Most notably code reuse, inheritance and overloading. Shoud I still be writing fuctions in my scripts or do classes make them obsolete? Is there a rule of thumb that if your script has say 20 lines or more you should write a class?

Recommended Answers

All 4 Replies

Classes don't make functions obsolete. The beauty of Python is that you can go either way. It's up to you, if a class makes sense, use it. I use classes in most GUI programs, using inheritance is very common. Also, the program looks more organized and simply makes life easier.

I use a class 99% of the time. Once the program gets beyond the simple function level, it organizes the code IMHO. Also, a class doesn't have any of the global variable problems.

I find functions still a great thing to have if you want to have something unrelated to either class. Personally i like to only have related functions in a class and if i want a function and there is no specific class that it fits into then i do not put it in a class and leave it out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.