Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 9th, 2005, 1:32 AM   #1
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 3 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
Cool Variables

OK, i am making a simple program in python, all was going well until the variables didnt work. The actual code is a bit to long to post and it is very messy, so i made this example of what error i am getting.
#defining the variable
msg = "Hello"
#function to chnage the msg variable to the argument in the funtion
def changeit(new):
	msg = new
	return msg
#calling the function with the argument "goodbye"
changeit("Goodbye")
#Printing the variable to check that it has changed acording to the function that we called
print msg

You see basically, i have declared a variable, i then have made a funtion that should change that variable, i then call that funtion and print the variable.

Basically, the msg variable stays as "Hello" and doesnt change. I think it is something to do with Global variables etc. but im not sure how to correct it, i tried return msg but it doesn't work.

I have searched google (maybe i am searching for the wrong words?) and i checked the python documentation but i cannot find the answer to my question.

How can i fix it so that it prints the variable from the function?
coldDeath is offline   Reply With Quote
Old Aug 9th, 2005, 7:26 AM   #2
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile

You have a function that returns something so you need to assign it to a variable when you call it. This should do it:
#defining the variable
msg = "Hello"
#function to change the msg variable to the argument in the funtion
def changeit(new):
	msg = new
	return msg
#calling the function with the argument "goodbye"
msg = changeit("Goodbye")
#Printing the variable to check that it has changed acording to the function that we called
print msg
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Aug 9th, 2005, 7:27 AM   #3
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
#defining the variable
msg = "Hello"
#function to chnage the msg variable to the argument in the funtion
def changeit(new):
	msg = new
	return msg
#calling the function with the argument "goodbye"
msg = changeit("Goodbye")    #This line was changed
#Printing the variable to check that it has changed acording to the function that we called
print msg

Return from a function returns the variable to whatever called the function. The way you had it setup, the new value wasn't being returned to any variable, and the temporary variables were therefore not used.

For this part:
def changeit(new):
	msg = new
	return msg

Could be made:
def changeit(new):
	return new

Because declaring msg doesn't do anything anyways. Inside a function, only the variables that were changed and created within that function will operate, none will exist outside that function or the next time you visit the function again. The only way to ever see it again is with the command global, or using return and then passing it back as a paramater.

Here is another way of looking at your program that is slightly more advanced:

class variable(object): #variable can be changed to any word

    #this will initiate upon declaring variable
    def __init__(self, dec_variable):

        #self is the collection of variables within the class, dec_variable is the paramater passed when the class is declared
        self.text = dec_variable #self makes the variable able to be manipulated within the class, self.text = dec_variable is making "Hello" accessable by the other functions in this class

    def changeit(self, new_variable):

        #self is the collection of variables within the class, new_variable is the paramater passed when changeit is called
        self.text = new_variable #self.text which was declared in a different function, can still be manipulated here. It is redeclared as "Goodbye"


#Declare msg as a reference of the class variable
msg = variable("Hello")

#Access the function changeit in the class variable (since variable is referenced with msg)
msg.changeit("Goodbye")

#Print the variable variable.text (since variable is referenced with msg)
print msg.text
Sane is offline   Reply With Quote
Old Aug 9th, 2005, 8:23 AM   #4
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 3 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
Thanks for your help, just after i posted this topic, "the python bible" book form amazon.co.uk got delivered and i learnt how to do object oriented python, so i managed to sort out my problem.

It was a stupid error to make in the first place, thanks dietrich for sorting that out.
Sane that advanced way looks good, mine looks a bit like that
coldDeath is offline   Reply With Quote
Old Aug 9th, 2005, 11:35 AM   #5
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
Hey I sorted it out too! Just 30 seconds after dietrich did .
Sane is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:33 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC