Gatsby Great
Gatsby Great

Reputation: 45

python stdout and stderr: Am confused how my file object get passed to when i didn't declare global

I am having problem understand the sys.stdout and sys.stderr

The problem is how did the variable put get got into the cmd module? My aim is to write a single function that would accept a string basically I am using it to write exception caught in my application to the screen and to a log file. I saw similar code somewhere so I decided to learn more by using the same example i saw which was a little different from my as the other person was writing to tow files simultaneously with just one function call.

According to my understanding:

The cmd module recieves a string which it then calls output module on the recieved string. output module takes two arguements - (1 of its parameters must evalute to python standard input module object and second the a string) fine. However, since output module calls logs module which does the printing or better still combines parameters by calling write function from python's standard output object passing it the string or text to be written.

Please if my explanation is not clear it means I am truely not understanding the whole process.

My questions is: How does put variable called outside the function got into the cmd module or any other module when I have commented it or not even called out?

Please find code below `

import sys

def logs(prints, message):
    #global put
    #print prints
    prints.write(message)
    prints.flush()

def output(prints, message):
    #global put
    #logs(prints, content)
    logs(prints, message)


    #logs(put, via)
''' This is where the confusion is, how did put get into this function when i did
not declare it...'''
def cmd(message):
    #global put

    output(put, message)
    output(sys.stderr, message)

put = open('think.txt', 'w')

#print put, '000000000'

cmd('Write me out to screen/file')
put.close()

`

Upvotes: 0

Views: 192

Answers (2)

tdelaney
tdelaney

Reputation: 77337

Its because of the way that python handles scopes. When you execute the script, the logs, output and cmd functions are defined in the module namespace. Then put = open('think.txt', 'w') creates a variable called put in the module namespace.

When you call cmd, you are now executing in the function's local namespace. it is created when the function is called and destroyed when the function exits. When python hits the expression output(put, message), it needs to resolve the names output, put and message to see what to do with them. The rules for a function are that python will look for the name in the local function namespace and then fall back to the global module namespace if the variable is not found.

So, python checks the function namespace for output, doesn't find anything, looks at the module namespace and finds that output refers to a function object. It then checks the function namespace for put, doesn't find anything, looks at the module namespace and finds that put refers to an open file object. Finally, it looks up message, finds it in the function namespace (the function parameters go into the function namespace) and off it goes.

Upvotes: 1

khampson
khampson

Reputation: 15296

put is declared as a global variable, so when you access it from within cmd, it is accessing that global variable without you needing to declare it within the function.

For example, this code prints 5 for the same reason:

def foo():
    print "bar: {0}".format(bar)

bar = 5

foo()

Upvotes: 1

Related Questions