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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:46:15+00:00 2026-05-14T05:46:15+00:00

I’m just starting on Python and maybe I’m worrying too much too soon, but

  • 0

I’m just starting on Python and maybe I’m worrying too much too soon, but anyways…

log = "/tmp/trefnoc.log"

def logThis (text, display=""):
    msg = str(now.strftime("%Y-%m-%d %H:%M")) + " TREfNOC: " + text
    if display != None:
        print msg + display
    logfile = open(log, "a")
    logfile.write(msg + "\n")
    logfile.close()
    return msg

def logThisAndExit (text, display=""):
    msg = logThis(text, display=None)
    sys.exit(msg + display)

That is working, but I don’t like how it looks. Is there a better way to write this (maybe with just 1 function) and is there any other thing I should be concerned under exiting?


Now to some background (but not about trefnoc)…

Sometimes I will call logThis just to log and display. Other times I want to call it and exit. Initially I was doing this:

logThis ("ERROR. EXITING")
sys.exit()

Then I figured that wouldn’t properly set the stderr, thus the current code shown on the top.

My first idea was actually passing “sys.exit” as an argument, and defining just logThis ("ERROR. EXITING", call=sys.exit) defined as following (showing just the relevant differenced part):

def logThis (text, display="", call=print):
    msg = str(now.strftime("%Y-%m-%d %H:%M")) + " TREfNOC: " + text
    call msg + display

But that obviously didn’t work. I think Python doesn’t store functions inside variables. I couldn’t (quickly) find anywhere if Python can have variables taking functions or not! Maybe using an eval function? I really always try to avoid them, tho. Sure I thought of using if instead of another def, but that wouldn’t be any better or worst.

Anyway, any thoughts?

  • 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-14T05:46:15+00:00Added an answer on May 14, 2026 at 5:46 am

    There’s no reason for “logThisAndExit”, it doesn’t save you much typing over

    sys.exit(logThis(text)+display)
    

    (compare logThisAndExit(text, display))

    or

    sys.exit(logThis(text))
    

    (compare logThisAndExit(text))

    Not that I’m entirely sure why you like your exit messages formatted as log lines.

    In answer to your original question: you’re missing parentheses: call(msg+display) works fine. But I think that’s waaaay overengineering for logging/exiting stuff. Anyone who maintains your code will have to understand your function to know when it’s exiting and when it’s not.

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

Sidebar

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.