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

  • Home
  • SEARCH
  • 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 3852564
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:20:12+00:00 2026-05-19T17:20:12+00:00

I often write the following code: print(‘object’, ‘moved’, ‘from =’, object.location, ‘dest =’, new_location)

  • 0

I often write the following code:

print('object', 'moved', 'from =', object.location, 'dest =', new_location)
print('object', 'created', 'at =', object.location, 'status =', status)

and so on.

I was planning to replace all of these with a function log:

def log(self, action, **details):
  print('object', action, end = '')
    for var, value in state.items():
      print(var, '=', value, end = '')
    print()

This almost works – except for two problems. First, I can’t control the order of the output, and it’s important to me. Of course, details is just a dictionary, so the order of the parameters is lost. Second, I can’t use any language keywords, e.g., from as the name of the keyword argument.

Is there any solution that avoid unnecessary verbosity but doesn’t suffer from these problems?

The reason I want to use log function is that I may want to disable / enable it or change the format in a single location.

  • 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-05-19T17:20:12+00:00Added an answer on May 19, 2026 at 5:20 pm

    You know about the logging module, I take it?

    I would just use preformed messages, and substitute the necessary bits in:

    movedmsg = "object moved from = %s to = %s"
    print(movedmsg % (object.location, new_location))
    

    You might prefer the new style of string formatting:

    movedmsg = "object moved from = {0} to = {1}"
    print(movedmsg.format(object.location, new_location))
    

    If you did need to do it with a dictionary-like structure, you could make an OrderedDict. But that is a bit more verbose. And it’s not really the “one obvious way to do it”.

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

Sidebar

Related Questions

Often, programmers write code that generates other code. (The technical term is metaprogramming ,
I often find I want to write code something like this in C#, but
I often see JavaScript code where a function may take in an options object
I noticed I often write the following: class X: def __init__(self, var1, var2, var3):
In my classes I often write a quick operator!= by returning !(*this == rhs)
I always work in windows environment and most often to write the program in
Often, I find myself wanting to write a unit test for a portion of
This question is inspired by this question , which features the following code snippet.
When I run the following code, it only seems to be downloading the first
My reasons for the idea/proposal are following: 1) Console.WriteXX() is possibly used more often

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.