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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:00:11+00:00 2026-06-13T10:00:11+00:00

So, there is no explicit C#- or Java-style StringBuilder class provided with Python. Note,

  • 0

So, there is no explicit C#- or Java-style StringBuilder class provided with Python.
Note, I am a bit new to Python, so I am not sure if my idea is a good one.

Suppose I want to have a decorator (if possible) that would join the iterables together. This particular example can be implemented differently, but I can think of other use cases. So …

@string_builder
def build_insert_statement(table_name, col_names, values, where_clause):
    yield 'insert into '
    yield table_name
    yield ' ( '
    yield ', '.join(col_names)
    yield ' ) values ( '
    yield ', '.join(values)
    yield ' ) where '
    yield where_clause
    yield ';'

Again, I know that there are alternative ways to do this. However, can this be done? Can a decorator always respect the original function’s signature or only sometimes or not at all? If this is possible, then is it a heresy? Why?

  • 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-13T10:00:13+00:00Added an answer on June 13, 2026 at 10:00 am

    The following definition for string_builder should work:

    from functools import wraps
    
    def string_builder(func):
        @wraps(func)
        def wrapped(*args, **kwargs):
            return ''.join(func(*args, **kwargs))
        return wrapped
    

    Using the function defined in your question with string_builder as a decorator:

    >>> build_insert_statement('foo', ['bar'], ['1'], 'baz')
    'insert into foo ( bar ) values ( 1 ) where baz;'
    

    Note that using [functools.wraps][1] is not necessary, but it is good practice because without it the name of the function (build_insert_statement.__name__) after decoration would be “wrapped” instead of “build_insert_statement”.

    So it is possible, is it a good idea?

    Not for your example, but you said you have other use cases. A decorator like this is not heresy, but you shouldn’t overuse decorators because it can obscure the implementation.

    If you are just worried about line length, consider one of the following alternatives:

    • Using ''.join() on a list:

      def build_insert_statement(table_name, col_names, values, where_clause):
          return ''.join(['insert into ',
                          table_name,
                          ' ( ',
                          ', '.join(col_names),
                          ' ) values ( ',
                          ', '.join(values),
                          ' ) where ',
                          where_clause,
                          ';'])
      
    • Combining the strings with +:

      def build_insert_statement(table_name, col_names, values, where_clause):
          return ('insert into ' +
                  table_name +
                  ' ( ' +
                  ', '.join(col_names) +
                  ' ) values ( ' +
                  ', '.join(values) +
                  ' ) where ' +
                  where_clause +
                  ';')
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Which of the methods of java Vector class is synchronized.Since there is no explicit
Is there a keyword in java to allow explicit invocation of members of the
I have the following snippet of java code: final Class<?> junitCoreClass = AccessController.doPrivileged( new
Am not sure my title is explicit enough. here is what i mean.I'm developing
Could someone please explain to me why there is explicit need to assign generic
Situation : I have a persistable class with variable of java.util.Date type: import java.util.Date;
Inside a Java enumerated class, I'd like to create a final static array containing
Java has default constructor but is not functioning in the same way as C++.
Java programmers and API seems to favor explicit set/get methods. however I got the
In Java, there is ThreadLocal, which can be used to carry some data from

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.