Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the advantage to define some function out of class in python program [closed]

Why should functions be declared outside of the class they're used in in Python?

For example, the following project on Github does this with its _hash, _task_from_taskline, and _tasklines_from_tasks functions. The format is the same as the following:

class UnknownPrefix(Exception):
"""Raised when trying to use a prefix that does not match any tasks."""
def __init__(self, prefix):
    super(UnknownPrefix, self).__init__()
    self.prefix = prefix


def _hash(text):

    return hashlib.sha1(text).hexdigest()

def _task_from_taskline(taskline):
    """
    snipped out actual code
    """
    return task

def _tasklines_from_tasks(tasks):
    """Parse a list of tasks into tasklines suitable for writing."""


    return tasklines

But I think these functions have a relation with the class TaskDict.

Why put them out of the class? What is the advantage of having them declared outside of the class?

like image 378
poleworld Avatar asked May 16 '13 14:05

poleworld


3 Answers

The Stop Writing Classes PyCon talk is not exactly on this subject but includes what I feel are related lessons here: basically, the idea is that classes are for creating objects. All of the attributes, instance methods and class methods should further the goal of creating objects or making the objects work. Classes are not for code organization (under this theory) -- that's what modules are for.

This is a very opinionated strategy and not everyone agrees with it. But if you think about your example in this context, it's clear that the reason the functions aren't part of the class is that even though the functions are used by the class, they don't actually operate on any object or further the creation of any object directly. They're just utility functions that aren't bound to any specific class, and could theoretically be used elsewhere in the library.

So why or why not put them in the class? It boils down to whether you believe that classes should be used for code organization or not. In this case, the author apparently bought into the idea that it's modules that are for code organization, not classes.

like image 189
Andrew Gorcester Avatar answered Nov 15 '22 06:11

Andrew Gorcester


  1. They are easier to import and have shorter name than member functions
  2. Even if you put them in the class, they're probably going to be a classmethod/staticmethod anyway.
  3. Pythonic code uses duck-typing; a method would imply that it can only be used with that class, while in fact the code may be more generic than that.
  4. Personal preference. Some people liked to separate between the "domain model" and the "business logic".

In the end, for this particular program, there is really no benefit nor disadvantage for doing either way.

like image 44
Lie Ryan Avatar answered Nov 15 '22 06:11

Lie Ryan


functions has small performance advantage

>>> import timeit
>>> timeit.Timer('foo()', 'def foo(): return 1').timeit()
0.09944701194763184
>>> timeit.Timer('A.foo()', '''
... class A(object):
...     @staticmethod
...     def foo():
...         return 1''').timeit()
0.12048101425170898
like image 39
oleg Avatar answered Nov 15 '22 05:11

oleg