List Info

Thread: Setting a global variable on class initialisation




Setting a global variable on class initialisation
user name
2006-04-27 15:22:11
[Max Russell]

| I know that overall Global variables are  bad idea,
however, 
| I have a situation where on on initialisation of a class,
I 
| need to read in two files and then populate two lists.
| The lists need to would appear to need to be outwith the 
| class I am working with and global.
|  
| Can anyone give good examples of this?

Well, in simple terms this *sounds* like what
you're trying to do (not attempting any more
Python approach at this juncture):

<module>
l1 = []
l2 = []

class X:
  def __init__ (self, filename1, filename2):
    l1.extend (open (filename1).readlines ())
    l2.extend (open (filename2).readlines ())

if __name__ == '__main__':
  open ("test1.txt", "w").write
("hello\nworld\n")
  open ("test2.txt", "w").write
("goodbye\nworld\n")
  x = X ("test1.txt", "test2.txt")
  print l1
  print l2

</module>

Obviously this code doesn't do anything terribly
useful, but I wanted to see if it was more-or-less
what you had in mind?

TJG

____________________________________________________________
____________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a
proactive
anti-virus service working around the clock, around the
globe, visit:
http://www.star.net.uk
____________________________________________________________
____________
_______________________________________________
Tutor maillist  -  Tutorpython.org
http://
mail.python.org/mailman/listinfo/tutor
[1]

about | contact  Other archives ( Real Estate discussion Medical topics )