Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
two different doc strings
#1
i have a case where two different functions for different purposes were re-factored in a way that they shared exactly identical code. so i am looking at making them share a single definition:
def foo(...arguments...):
    ...the code...
    return
bar = foo
the original two functions had docstrings that explained the different purposes of those two functions. what i would like to know is if there is a way to associate two different docstrings with the two different function names now that they share identical code.

for those who cannot understand how two different purposes can be handled by exactly the same code, it turns out that a common value somewhere had the proper value in both cases and these functions used that value to carry out their purposes in the new version.
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
I don't think a docstring should describe the purpose of function. It should describe what it does and how it is used. What it is used for should direct the design, but the more your code can be used for multiple purposes the better.
Reply
#3
that project has many functions that are similar. i may end up with more duplications like that. i agree with your statement about the functions. if i leave foo() and bar() as separate definitions, i can give each function its own docstring that describes what it does and how it is used. but i don't think it is good to have these two functions implemented with exactly the same code.

maybe, that is a wrong idea. it could be implementation versus expression that i need to consider.

do i need to focus more or less on efficiency, the efficiency of saving a function definition that is about 5 lines. my long term habitual practice has been for efficiency. but in Python, i am try to move away from that although there are cases where the code can do less if i have things work a different way. so i still do enough optimizing to avoid wasteful expressions.

this thinking comes from my decades of programming in assembly and C (no C++) and little bit of PLC and Pike.
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
  Trying to understand strings and lists of strings Konstantin23 2 776 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,780 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Finding multiple strings between the two same strings Slither 1 2,530 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  lists, strings, and byte strings Skaperen 2 4,242 Mar-02-2018, 02:12 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