473,385 Members | 1,347 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Variable scope within a class

How do I make a variable available to code *inside* my class in such a way
that it is available to all defs in the class? In my example below f is not
accessible within the showVars method.

class myClass:
f = [1,2,3]

def showVars ( self ):
print f # Error here as f does not exist

def main():
x = myClass()
print x.f
x.showVars()

if __name__ == "__main__":
main()
Thanks to anyone who can offer some guidance to a Python beginner!
Graham.

Jul 18 '05 #1
3 1774
On Tue, 03 Feb 2004 10:11:43 +0000, Graham wrote:
How do I make a variable available to code *inside* my class in such a way
that it is available to all defs in the class? In my example below f is not
accessible within the showVars method.


It depends on whether you want to have a class variable or an instance
variable; looking at the following variation of your code should make
clear the difference:

class myClass:
f = [1, 2, 3]

def showVars(self):
print self.f
class myClass2:
def __init__(self):
self.f = ['a', 'b']

def showVars(self):
print self.f
print myClass.f

myc = myClass()
myClass.f = [4, 5, 6]
myc.showVars()

myc2 = myClass2()
myClass2.f = ['c', 'd'] # this f is created here!!!
print myClass2.f
myc2.showVars()
+++ OUTPUT +++

[1, 2, 3]
[4, 5, 6]
['c', 'd']
['a', 'b']
Remark: 'Normally', you want something like myClass2.

HTH / Nuff

Jul 18 '05 #2
Hi Graham,
How do I make a variable available to code *inside* my class in such a way
that it is available to all defs in the class? In my example below f is not
accessible within the showVars method.

class myClass:
f = [1,2,3]

def showVars ( self ):
print f # Error here as f does not exist


Since f is a class variable, it is also an instance variable. You can
either reach it by referring to the class or by referring to the
instance. The former can be done with 'self.__class__.f', the latter by
the simpler 'self.f'. The difference is that, if self.f changes, this is
true for only 1 instance of the class, while all share the same
self.__class__.f.

Gerrit.

--
PrePEP: Builtin path type
http://people.nl.linux.org/~gerrit/c.../pep-xxxx.html
Asperger's Syndrome - a personal approach:
http://people.nl.linux.org/~gerrit/english/

Jul 18 '05 #3
Thanks Gerrit and Nuff Said. Very helpful indeed.

Graham
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Brian Jones | last post by:
I'm sure the solution may be obvious, but this problem is driving me mad. The following is my code: class a(object): mastervar = def __init__(self): print 'called a'
7
by: YGeek | last post by:
Is there any difference between declaring a variable at the top of a method versus in the code of the method? Is there a performance impact for either choice? What about if the method will return...
6
by: Joe Molloy | last post by:
Hi, I'm wondering is there any way I can get a variable's value from within a class when the variable has been declared outside the class but in the same script as the class is contained in. ...
4
by: Gery D. Dorazio | last post by:
Gurus, If a static variable is defined in a class what is the scope of the variable resolved to for it to remain 'static'? For instance, lets say I create a class library assembly that is...
24
by: LP | last post by:
After a code review one coworker insisted that global are very dangerous. He didn't really give any solid reasons other than, "performance penalties", "hard to maintain", and "dangerous". I think...
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
5
by: strawberry | last post by:
In the function below, I'd like to extend the scope of the $table variable such that, once assigned it would become available to other parts of the function. I thought 'global $table;' would solve...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
0
MMcCarthy
by: MMcCarthy | last post by:
We often get questions on this site that refer to the scope of variables and where and how they are declared. This tutorial is intended to cover the basics of variable scope in VBA for MS Access. For...
2
by: ray | last post by:
Hi, all, foreach($array as $k =$v) { $foo = ...; } echo $foo; Is it allowed to access the $foo variable that is created within the loop from outside of the loop? I think it isn't allowed,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.