Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing static methods in python - best practice
    text
    copied!<p>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 <a href="https://stackoverflow.com/questions/14992474/factory-method-for-python-object-best-practice">Factory method for python object - best practice</a>).</p> <p>Lets say I have a class used to represent some entity data in a database. Imagine the data is a <code>dict</code> object containing field names and field values and one of the fields is an ID number that makes the data unique.</p> <pre><code>class Entity(object): def __init__(self, data, db_connection): self._data = data self._db_connection </code></pre> <p>Here my <code>__init__</code> method takes the entity data <code>dict</code> object. Lets say I only have an ID number and I want to create an <code>Entity</code> instance. First I will need to find the rest of the data, then create an instance of my <code>Entity</code> object. From my previous question, we established that using a class method as a factory method should probably be avoided when possible.</p> <pre><code>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) </code></pre> <p>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.</p> <pre><code>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) </code></pre> <p>Or does it make more sense to use a standalone function to find the entity data from an ID number.</p> <pre><code>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) </code></pre> <p>Note: I do not want to change my <code>__init__</code> method. Previously people have suggested making my <code>__init__</code> method to look something like this <code>__init__(self, data=None, id_number=None)</code> 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?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload