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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:33:15+00:00 2026-05-23T17:33:15+00:00

a.py #!c:/Python27/python.exe -u from connection import Connection import globals globals.server_ip = ‘192.168.0.1’ connection =

  • 0

a.py

#!c:/Python27/python.exe -u

from connection import Connection
import globals

globals.server_ip = '192.168.0.1'
connection = Connection()

globals.py

#!c:/Python27/python.exe -u

server_ip = '127.0.0.1'

connection.py

import globals

class Connection:        
    def __init__(self, server_ip = globals.server_ip):
        print 'Connection is ' + server_ip + '\n'

I was expecting I will be getting Connection is 192.168.0.1 being printed. But, instead, Connection is 127.0.0.1 is being printed.

Unless I try to construct the connection by passing in the parameter explicitly (which is not something I wish to, as I am reluctant to make change any more on Connection with 0 parameter)

connection = Connection(globals.server_ip)

Why is this so? Is there any other techniques I can apply?

  • 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-23T17:33:16+00:00Added an answer on May 23, 2026 at 5:33 pm
    def __init__(self, server_ip=globals.server_ip):
    

    The argument is bound when the method is created and not re-evaluated later.
    To use whatever is the current value, use something like this:

    def __init__(self, server_ip=None):
        if server_ip is None:
            server_ip = globals.server_ip
    

    Btw, for exactly the same reason a function like this would be likely to not work as intended:

    def foobar(foo=[]):
        foo.append('bar')
        return foo
    

    In performance-critical code this behaviour can also be used to avoid global lookups of builtins:

    def highspeed(some_builtin=some_builtin):
        # in here the lookup of some_builtin will be faster as it's in the locals
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following files dummy.py #!c:/Python27/python.exe -u from mako import exceptions from mako.template
I'm using Python 2.6 and PyGTK 2.22.6 from the all-in-one installer on Windows XP,
I want to add QSplashScreen to PyQT4 application. It works fine from Python, but
I'm using the pywin32-216.win32-py2.6.exe package to install pywin32 on Python 2.6.5 (r265:79096, Mar 19
I just installed the Python 2.7.1 on Windows Vista using installer from official site
I've written a very simple code in python and generated one .exe with py2exe.
Possible Duplicate: Getting python.exe path at run time I have a python app that
I've been trying to import a p4 depot path to git using git-p4 python
Attempting to build .exe using py2exe: python setup.py py2exe , receiving error: ImportError: No
I'm trying to open a Python script from the Python command line. There's a

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.