Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using global variable as default parameter

Tags:

python

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?

like image 315
Cheok Yan Cheng Avatar asked Jul 14 '11 07:07

Cheok Yan Cheng


People also ask

Why should you avoid using global variables?

Using global variables causes very tight coupling of code. Using global variables causes namespace pollution. This may lead to unnecessarily reassigning a global value. Testing in programs using global variables can be a huge pain as it is difficult to decouple them when testing.

Can a parameter be a global variable?

The function parameter gets a copy of the global variable. It itself (the parameter) is a local variable of the function. Any changes of the local variable do not influence on the original argument because the function deals with a copy of the value of the global variable.

How do you set a parameter to default value?

The default parameter is a way to set default values for function parameters a value is no passed in (ie. it is undefined ). In a function, Ii a parameter is not provided, then its value becomes undefined . In this case, the default value that we specify is applied by the compiler.

How do you set global parameters?

To create a global parameter, go to the Global parameters tab in the Manage section. Select New to open the creation side-nav. In the side-nav, enter a name, select a data type, and specify the value of your parameter. After a global parameter is created, you can edit it by clicking the parameter's name.


1 Answers

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
like image 63
ThiefMaster Avatar answered Oct 06 '22 23:10

ThiefMaster