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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:04:56+00:00 2026-05-16T06:04:56+00:00

Example code from a module: somevar = a def myfunc(somevar = None): # need

  • 0

Example code from a module:

somevar = "a"

def myfunc(somevar = None):
    # need to access both somevars ???
    # ... if somevar was specified print it or use the global value
    pass

if __name__ == '__main__':
    somevar = "b" # this is just for fun here
    myfunc("c")
    myfunc() # should print "a" (the value of global variable)

There are at least two reasons for using the same name:educational (to learn how to use local/globals) and usage in modules.

Let say that this code is part of your module: mymodule and you want to do things like:

import mymodule

mymodule.samevar = "default"
...
mymodule.myfunc(somevar = "a")
...
mymodule.myfunc()

As you can imagine in this example is simplified, imagine that somevar parameter is one of many optional parameters and that myfunc is called in many places.

  • 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-16T06:04:57+00:00Added an answer on May 16, 2026 at 6:04 am

    By far the best approach, as the other answers say, is to avoid this very, very bad design: just don’t use the same names for two different things!

    If you’re locked into this terrible design, maybe because your company’s Supreme Architect decreed it and it’s just non-negotiable (e.g., there’s tons of customer code relying on the names of both the global and the parameter), the first order of business for you is to make sure your resume is up to date, print it on the best printer, and start getting in touch with members of your social network to look for a job at a place that’s run more sensibly; next, to avoid losing health insurance until a better job is found, try

    def myfunc(somevar = None):
        if somevar is None:
            print globals().get('somevar')
        else:
            print somevar
    

    I’ve also renamed your mufunc to myfunc because that’s the name it’s being called with (though given the incredibly bad design your hypothetical Supreme Architect seems to be able to perpetrate, maybe he’s also specified that the function be called by a different name from the one it’s defined with? seems to go with the systematic name conflict this hypothetical but already pretty hateful guy is assumed to have foisted on you!-).

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

Sidebar

Related Questions

I need to plot multiple sphere and I was using the example code from
How can you depend on test code from another module in Maven? Example, I
Sometimes I've seen people use module.exports.instance in their code ( example ). How does
I took the example code from the Kendo UI demos at http://demos.kendoui.com/web/grid/remote-data.html , binding
This is an example code from the prototype site. var url = '/proxy?url=' +
Please consider the following example code (from the lm doc): ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) trt
I'm trying to follow some example code from microsofts mdn site.. var WshShell =
With regards this example from Code Complete: Comparison Compare(int value1, int value2) { if
I copied this code from an example . I've read it 100 times. Array.prototype.map
I'm trying to implement webgl shadow mapping from an example code that already implements

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.