Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9026637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:34:14+00:00 2026-06-16T06:34:14+00:00

Background I have a library with several functions that read from or write to

  • 0

Background

I have a library with several functions that read from or write to files. Each function accepts the file as the first argument, either as a file object or a file name. Hence, all of the functions have the same piece of code at the beginning, similar to the following:

if isinstance(f, str):
    file_obj = open(f, 'w')
else:
    file_obj = f

Now I figured I could write this once in a decorator and wrap all the functions in it, and not repeat myself. (I’m also thinking of implementing the context manager interface within the same decorator.)

So if I do this, the functions will look like:

@file_aware('w')
def function(f, *args, **kwargs):
    """Do stuff. `f` can be file object or file name"""
    for line in f:
       ....

The question

My concern is that now the docstring of the function doesn’t correspond to the code below it.
(I’m planning to keep the docstring of the decorated function with functools.wraps.)
Does it reduce the readability/maintainability/transparency of the code?
From what I understand, decorators can easily come and go, but at the same time this one kinda changes the API (I’m not planning to remove the functionality). What’s the “best practice” way for this case?

I can think of processing the docstring automatically inside the decorator, but:

  • that’s not the most natural thing to do;
  • that only makes sense for online documentation, and won’t help (rather, the opposite) someone reading the source code.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T06:34:16+00:00Added an answer on June 16, 2026 at 6:34 am

    One option would be to pass the docstring into the decorator. That way you would still have a docstring by the function definition for reading source code but if you were to change or remove the decorator you wouldn’t end up with an incorrect docstring.

    For example:

    @file_aware(docstring="Do stuff. `f` can be file object or file name", mode="r")
    def function(f, *args, **kwargs):
        for line in f:
           ....
    

    Your file_aware decorator might then look something like this:

    def file_aware(docstring, mode):
        def deco(func):
            @functools.wraps(func)
            def wrapped(f, *args, **kwargs):
                if isinstance(f, str):
                    file_obj = open(f, mode)
                else:
                    file_obj = f
                return func(file_obj, *args, **kwargs)
            wrapped.__doc__ = docstring
            return wrapped
        return deco
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably a multi-part question. Background: we have a native (c++) library that
Background I have a function that takes a config object as an argument. Within
Background : I have a few different threads which each need to write to
Background I have a dimension table that has a single record for each day.
I have an event that is raised from a third-party library, which executes on
Yet another compatibility problem from me. BACKGROUND I have an application that works in
Background: I have a number of scripts that parse log files looking for hex
Background: I'm using Google's protobuf , and I would like to read/write several gigabytes
I have several images that claim to have a transparent background but are actually
Background I have two lists, the first is items which contains around 250 tuples,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.