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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:22:08+00:00 2026-06-17T15:22:08+00:00

I’m a bit of a beginner to coding in Python, which is a bit

  • 0

I’m a bit of a beginner to coding in Python, which is a bit of a jump for me coming from Java. The question I have right now is about the use of global variables in Python for constants and such.
In Java, we sort of have two ideas for constants, we can have something like this:

private static final HOME_URL_CONST = "localhost:8080";

Or if we need to assign the value at runtime:

private static HOME_URL = "";
public void init(){ 
  HOME_URL = "localhost:8080"; 
}

The point is that in the latter case, once you set the static variable, it stays set. However in Python, this is not the case. If I create a global variable and then assign it in a function, that variable will only have the assigned value inside that function.
Right now i have something like this:

def initialize():
  global HOME_URL
  with open("urls.txt", 'rb') as f:
    HOME_URL = json.load(f.read())['urls']

is this an acceptable method of doing this or are there some repercussions and side effects I’m not aware of?

  • 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-17T15:22:09+00:00Added an answer on June 17, 2026 at 3:22 pm

    In Python, there is no definition of const variable per se’, because of its dynamic nature. Constants are dictated through Style and thus quoting from PEP 8

    Constants are usually defined on a module level and written in all
    capital letters with underscores separating words. Examples include
    MAX_OVERFLOW and TOTAL.

    So, if you want a variable to be used as constant, define it at module level, name it with uppercase separated by underscore and follow the convention, so that there are no other variables in any other scope that conflicts with the constant variable.
    In any case you will not need any global qualifier, as a variable defined at module level would in any case would be in scope at the function level.

    So in this particular case

    HOME_URL = "localhost:8080"
    def initialize():
          #global HOME_URL #You don't need this
          home_url = HOME_URL
          with open("urls.txt", 'rb') as f:
               #Constants are not supposed to mutate
               home_url = json.load(f.read())['urls']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
I have a text area in my form which accepts all possible characters from
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have a jquery bug and I've been looking for hours now, I can't
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I have an autohotkey script which looks up a word in a bilingual dictionary
This could be a duplicate question, but I have no idea what search terms
I have an array which has BIG numbers and small numbers in it. I
I have been unable to fix a problem with Java Unicode and encoding. The
I have thousands of HTML files to process using Groovy/Java and I need to

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.