Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any advantage in using a Python class?

Tags:

I have a Python class full of static methods. What are the advantages and disadvantages of packaging these in a class rather than raw functions?

like image 828
Mat Avatar asked Jan 18 '09 22:01

Mat


People also ask

Are classes in Python useful?

Classes are great if you need to keep state, because they containerize data (variables) and behavior (methods) that act on that data and should logically be grouped together. This leads to code that is better organized (cleaner) and easier to reuse.

Why are classes better than functions Python?

Classes getting passed around (since they're objects) take a lot more computational power than calling a function and passing a string or two. Proper naming conventions on functions can do pretty much everything creating a class can do, and with only a fraction of the overhead and better code readability.

What is a Python class?

A Python class is like an outline for creating a new object. An object is anything that you wish to manipulate or change while working through the code. Every time a class object is instantiated, which is when we declare a variable, a new object is initiated from scratch.

What are the advantages of classes in programming?

Classes support a powerful programming model by encapsulating related functionality into objects. The benefit of organized code is especially important for maintenance, where changes or enhancements can be limited to the objects that are affected by the change. Classes enhance code reuse.


1 Answers

There are none. This is what modules are for: grouping related functions. Using a class full of static methods makes me cringe from Javaitis. The only time I would use a static function is if the function is an integral part of the class. (In fact, I'd probably want to use a class method anyway.)

like image 74
Benjamin Peterson Avatar answered Sep 20 '22 04:09

Benjamin Peterson