When and how are static methods suppose to be used in python? We have already established using a class method as factory method to create an instance of an object should be avoided when possible. In other words, it is not best practice to use class methods as an alternate constructor (See Factory method for python object - best practice).
Lets say I have a class used to represent some entity data in a database. Imagine the data is a dict
object containing field names and field values and one of the fields is an ID number that makes the data unique.
class Entity(object):
def __init__(self, data, db_connection):
self._data = data
self._db_connection
Here my __init__
method takes the entity data dict
object. Lets say I only have an ID number and I want to create an Entity
instance. First I will need to find the rest of the data, then create an instance of my Entity
object. From my previous question, we established that using a class method as a factory method should probably be avoided when possible.
class Entity(object):
@classmethod
def from_id(cls, id_number, db_connection):
filters = [['id', 'is', id_number]]
data = db_connection.find(filters)
return cls(data, db_connection)
def __init__(self, data, db_connection):
self._data = data
self._db_connection
# Create entity
entity = Entity.from_id(id_number, db_connection)
Above is an example of what not to do or at least what not to do if there is an alternative. Now I am wondering if editing my class method so that it is more of a utility method and less of a factory method is a valid solution. In other words, does the following example comply with the best practice for using static methods.
class Entity(object):
@staticmethod
def data_from_id(id_number, db_connection):
filters = [['id', 'is', id_number]]
data = db_connection.find(filters)
return data
# Create entity
data = Entity.data_from_id(id_number, db_connection)
entity = Entity(data)
Or does it make more sense to use a standalone function to find the entity data from an ID number.
def find_data_from_id(id_number, db_connection):
filters = [['id', 'is', id_number]]
data = db_connection.find(filters)
return data
# Create entity.
data = find_data_from_id(id_number, db_connection)
entity = Entity(data, db_connection)
Note: I do not want to change my __init__
method. Previously people have suggested making my __init__
method to look something like this __init__(self, data=None, id_number=None)
but there could be 101 different ways to find the entity data so I would prefer to keep that logic separate to some extent. Make sense?
The glib answer is: Not very often.
The even glibber but not quite as useless answer is: When they make your code more readable.
First, let's take a detour to the docs:
So, when you need a static method in C++, you need a static method in Python, right?
Well, no.
In Java, there are no functions, just methods, so you end up creating pseudo-classes that are just bundles of static methods. The way to do the same thing in Python is to just use free functions.
That's pretty obvious. However, it's good Java style to look as hard as possible for an appropriate class to wedge a function into, so you can avoid writing those pseudo-classes, while doing the same thing is bad Python style—again, use free functions—and this is much less obvious.
C++ doesn't have the same limitation as Java, but many C++ styles are pretty similar anyway. (On the other hand, if you're a "Modern C++" programmer who's internalized the "free functions are part of a class's interface" idiom, your instincts for "where are static methods useful" are probably pretty decent for Python.)
But if you're coming at this from first principles, rather than from another language, there's a simpler way to look at things:
A
@staticmethod
is basically just a global function. If you have a functionfoo_module.bar()
that would be more readable for some reason if it were spelled asfoo_module.BazClass.bar()
, make it a@staticmethod
. If not, don't. That's really all there is to it. The only problem is building up your instincts for what's more readable to an idiomatic Python programmer.And of course use a
@classmethod
when you need access to the class, but not the instance—alternate constructors are the paradigm case for that, as the docs imply. Although you often can simulate a@classmethod
with a@staticmethod
just by explicitly referencing the class (especially when you don't have much subclassing), you shouldn't.Finally, getting to your specific question:
If the only reason clients ever need to look up data by ID is to construct an
Entity
, that sounds like an implementation detail you shouldn't be exposing, and it also makes client code more complex. Just use a constructor. If you don't want to modify your__init__
(and you're right that there are good reasons you might not want to), use a@classmethod
as an alternate constructor:Entity.from_id(id_number, db_connection)
.On the other hand, if that lookup is something that's inherently useful to clients in other cases that have nothing to do with
Entity
construction, it seems like this has nothing to do with theEntity
class (or at least no more than anything else in the same module). So, just make it a free function.Here is a decent use case for @staticmethod.
I have been working on a game as a side project. Part of that game includes rolling dice based on stats, and the possibility of picking up items and effects that impact your character's stats (for better or worse).
When I roll the dice in my game, I need to basically say... take the base character stats and then add any inventory and effect stats into this grand netted figure.
You can't take these abstract objects and add them without instructing the program how. I'm not doing anything at the class level or instance level either. I didn't want to define the function in some global module. The last best option was to go with a static method for adding up stats together. It just makes the most sense this way.
Which would make the stat combination calls work like this
The answer to the linked question specifically says this:
So I don't know how you got the idea that using a classmethod is inherently bad. I actually like the idea of using a classmethod in your specific situation, as it makes following the code and using the api easy.
The alternative would be to use default constructor arguments like so:
I prefer the classmethod version that you wrote originally however. Especially since
data
is fairly ambiguous.Your first example makes the most sense to me:
Entity.from_id
is pretty succinct and clear.It avoids the use of
data
in the next two examples, which does not describe what's being returned; thedata
is used to construct anEntity
. If you wanted to be specific about thedata
being used to construct theEntity
, then you could name your method something likeEntity.with_data_for_id
or the equivalent functionentity_with_data_for_id
.Using a verb such as
find
can also be pretty confusing, as it doesn't give any indication of the return value — what is the function supposed to do when it's found the data? (Yes, I realizestr
has afind
method; wouldn't it be better namedindex_of
? But then there's alsoindex
...) It reminds me of the classic:I always try to think what a name would indicate to someone with (a) no knowledge of the system, and (b) knowledge of other parts of the system — not to say I'm always successful!