Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Instance Variables In Python

I'm new to Python, and I need to make a meathod, but I do not want the variables to be just local (I think it's called instance variables, but I'm not sure). How can I make this variable public to my whole program, not just the meathod I'm making with the "def" keyword?

Xcode 3-OTHER, Mac OS X (10.5.8)

Posted on May 7, 2012 4:09 PM

Reply
3 replies

May 8, 2012 11:53 AM in response to iTryFreeApps

Instance variables are members of Python classes. To create an instance variable for a Python class, declare the variable inside the class's __init__ method.


From reading your question, it sounds like you want to use global variables, not instance variables. To create a global variable declare it outside a method. To modify a global variable inside a method, declare the variable as global in the method.


x = 0


def do_something()

global x

x += 1


Using global variables should be avoided in most cases, especially for larger programs. For larger programs using classes and instance variables is better than using global variables, but you can get away with using global variables for small programs.

Instance Variables In Python

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.