Python Forum
writing a class or a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
writing a class or a function
#1
since an imported function (imported by itsekf or as part of a module) can save state/data between calls, there are many situations where a function is sufficient to meet my implementation goal. one case where i can see a need for a class is when i need to make multiple instances (each keeping separate state). and even if i need to share data between the instances, i can still use the class.

are there any other cases where a class is better, or essential, over a function?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
IMHO, the advantages of writing a function instead of a class are related to these entries of the Zen of Python
  1. Beautiful is better than ugly.
  2. Simple is better than complex.
  3. Flat is better than nested.
  4. Readability counts.
Besides that, I like classes very much and I never hesitate to write one instead of a function. Classes help with
  1. Complex is better than complicated.
  2. Sparse is better than dense.
  3. Practicality beats purity.
Moreover, they are indefinitely extensible and flexible.
Reply
#3
everything in python is a class, including functions:
Python 3.7.1 (default, Nov 20 2018, 18:13:14) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def zz():
...     print('abc')
... 
>>> type(zz)
<class 'function'>
>>> ww = {}
>>> type(ww)
<class 'dict'>
>>> a = 1
>>> type(a)
<class 'int'>
>>> quit()
Reply
#4
There are ways to retain data in a function that are not very clear. In those cases, I think a class is better to provide that clarity. Also, if you have two functions accessing the same stored data, that is generally better done as a class.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
a function retaining data looks clear to me. if there are 2 function that need to share or exchange data, they can be made in the same module and that would do it. OTOH, when i try to think of a use case for data sharing/exchanging, i end up with an idea that would be done better in a class, such as a queued buffer. potential users might need more than one. while that can still be done with just some functions exchanging retained data in a module, layered with a container like a dictionary, a class makes it simpler because the design expression is just for one instance of the many instead of having all that indexing in the code (BTDT in C and assembler).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
There are no cases where classes are required.

Python is a great example, being written in C, which itself doesn't support classes. It then follows, that anything in python is also possible in C, and that anything you could accomplish with a class, you could also accomplish without one.

So no, there are no cases where a class is essential over a function.

That said, they were created for a reason, and that reason is state management. If you have state you're keeping track of, classes help encapsulate that state, as well as grouping related functions together to strongly indicate to other readers that the functions are, in fact, related to one another.
Reply
#7
(Dec-28-2018, 08:00 PM)nilamo Wrote: It then follows, that anything in python is also possible in C, and that anything you could accomplish with a class, you could also accomplish without one.

It's more basic than that. Any Turing complete language can accomplish anything a class can accomplish. So Ook, the programming language of orangutans, can do anything a class can do. And Ook's only data structure is one list of integers. So as long as you have a list of integers, you don't need classes.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
Quote: being written in C, which itself doesn't support classes. It then follows, that anything in python is also possible in C, and that anything you could accomplish with a class, you could also accomplish without one.
If you look at C code from the 1970's, it is almost obvious that OOP was invented by C programmers. At that time they used to define C structures and a bunch of functions to operate on this structures. It only remained to invent inheritance and the vtable. I think C programming leads naturally to object oriented programming.
Reply
#9
I spent many years writing 'C'. We never called it 'Class', but the methods developed for structures truly were 'Classes'
just not called so.

I resisted C++ for many years, when I finally broke down and started using it in earnest I learned it's value. There's a place for classes, and a place where they don't belong.

I wouldn't consider writing instrument control software in C++.
Reply
#10
(Dec-28-2018, 08:00 PM)nilamo Wrote: There are no cases where classes are required.

Python is a great example, being written in C, which itself doesn't support classes. It then follows, that anything in python is also possible in C, and that anything you could accomplish with a class, you could also accomplish without one.

So no, there are no cases where a class is essential over a function.

but, or course, doing this could mak you Python code as ugly as the frequent mangled messes that often happen with C.

(Dec-28-2018, 08:00 PM)nilamo Wrote: That said, they were created for a reason, and that reason is state management. If you have state you're keeping track of, classes help encapsulate that state, as well as grouping related functions together to strongly indicate to other readers that the functions are, in fact, related to one another.
state can be encapsulated many ways. a class is not needed just for that. but when using a class, that code can gain benefits like duplication of that state.

is the "grouping related functions together" aspect being done by the functions being methods and being coded as attributes of the class so that the reader of the code sees the common usage of the class name? can't longer names do that, too, such as scaled_sin() and scaled_sqrt()

i'm just trying to pin down what all a class brings to the table.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  extracting a function/class/method from code Skaperen 5 2,246 Mar-30-2022, 12:13 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020