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 7738895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:23:05+00:00 2026-06-01T08:23:05+00:00

I’m working on a Pythonic command-line flashcard app for helping users learn different languages.

  • 0

I’m working on a Pythonic command-line “flashcard” app for helping users learn different languages. I would like to use Python’s cmd library to speed up development–of particular interest is the cmd.Cmd class’s do_help() method, which prints off the doc strings for the classes’s user methods. However, due to the multilingual nature of this application, I’d like to be able to drop in language-specific docstrings.

I read this SO question about using decorators, but I know very little about decorators, and I’d like to know if they are appropriate for my specific dilemma before investing a lot of time in learning about them.

What do you guys think? What is the best way of handling this situation?

Please let me know if you’d like more information on my problem.

  • 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-01T08:23:07+00:00Added an answer on June 1, 2026 at 8:23 am

    Decorators will be able to do what you want and more besides.

    Decorator primer

    As an additional bonus, decorators are very useful tools in a wide variety of situations. They’re really not that scary. In Essence, they are functions that take a function as argument and return a function. A very simple example prints the result of a call out:

    >>> def my_decorator(function):
    ...  def inner_function(*args, **kwargs):
    ...    res = function(*args, **kwargs)
    ...    print("We have: "+res)
    ...    return res
    ...
    >>> @my_decorator
    ... def add(x, y):
    ...  return x+y
    ...
    >>> add(1,2)
    We have: 3
    3
    

    This is the equivalent to

    add = my_decorator(add)
    

    For your problem, the decorator simply has to override the __doc__ property of the function.

    >>> def frenchmeup(fun):
    ...    fun.__doc__ = "Bonjour, documentation!"
    ...    return fun
    ... 
    >>> @frenchmeup
    ... def foo():
    ...   """hello doc"""
    ...   return "world"
    ... 
    >>> foo.__doc__
    'Bonjour, documentation!'
    

    Passing args into decorators

    This is pretty onerous if you have to create a decorator for each function. You could easily develop a general solution by using a documentation dictionary:

    >>> ttable = {
    ...   "FR" : {
    ...     "hello doc": "Bonjour, documentation!"
    ...   }
    ... }
    >>> def document(lang=None):
    ...   def doc_decorator(function):
    ...     if lang and lang in ttable:
    ...       function.__doc__ = ttable[lang][function.__doc__]
    ...     return function
    ...   return doc_decorator
    ... 
    >>> @document(lang="FR")
    ... def foo():
    ...   """hello doc"""
    ...   return 42
    ... 
    >>> foo.__doc__
    'Bonjour, documentation!'
    

    Not how the decorator is now generated by a function. This is more complex, but gives you the ability to pass arguments into the decorator.

    As a personal note, it took a little while before this clicked for me, but I now regularly use it in my python code.

    Strategy for automatic docstring translation

    You can actually generate the documentation dictionary programatically by inspecting your module for docstrings.

    From the comments:

    The idea is that the dictionary is automatically generated from your docstrings and then passed to a translator. If you changed the canonical (English?) docstring, then the translations would have to be changed too. By comparing the old translation table to the newly generated table, you would be able reinsert the translations for which the canonical docstring hasn’t changed. You’d only have to add the translation for the new docstring.

    So, for example, after changing the foo() docstring to """goodbye, doc...""" you would re-run your table generator, and you would get a new table, with the old “hello doc” key missing, and a new key-value pair ("goodbye, doc...", "") in your translation table.

    Alternative using the help_<cmd>() style for the cmd module

    If you’d rather use the help_<cmd>() style of the cmd module for the documentation, you can use the same principle of storing your translations in a dictionary and printing out the right translation based on the LANG variable in the help command.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.