A design question in Python: should it be one common function or two specific?

I am creating the base class of a database utility in Python. I recycle the old module into a class. Now I'm working on a function executeQuery(), and I'm not sure whether to keep the old design or change it. Here are two options:

  • (Old project :). It has one common method executeQuerythat executes a query for execution, and a boolean parameter committhat indicates whether to commit (insert, update, delete) or not (select) and determine if if, whether to commit or select and return.
  • (This is what I'm used to, but it may be because you cannot have a function that sometimes returns something, and sometimes not in the languages ​​I worked with :) Have 2 functions, executeQueryand executeUpdateQuery(or something like). executeQuerywill execute a simple query and return the result set, and executeUpdateQuerymake changes to the database (insert, update, delete) and not return anything.

Can the first method be used? It seems incomprehensible to me, but maybe this is more pythonic ...? Python is very flexible, maybe I should take advantage of this function, which cannot be executed this way in more strict languages ​​...

And the second part of this question, not related to the main idea - what is the best way to return query results in Python? Using this function, you can query the database, in what format ...?

+3
3

, , , - . , , , .

, Generator.

+2

FP, , , , , . , - , ( commit, , ).

... , , , , - - . ( ), .

+4

: , , - . , .

, , , , . , .

, , -. - :

with db.executeQuery('SELECT * FROM my_table') as results:
    for row in results:
        print row['col1'], row['col2']

... executeQuery ContextManager ( ), Generator. dict s.

+2

Source: https://habr.com/ru/post/1763663/


All Articles