Re: [Tutor] Setting a global variable on class initialisation
> Sorry I didn't make the problem I'm working with all too clear. Basically > I'm working with a class (Directory) that upon creation reads in a 3 files > and from these files populates one main list. > This bit is fine. > > The trickier bit was that I need to use two of the sublists (e.g the > contents of two of the files) outwith the module. If the lists are data within your Directory then any class that needs to use that data is treading in dubious water. Either that or the data shouldn't be in Directory! Are you sure that there isn't a service that Directory can perform on behalf of your new class? > class is created. The lists are accessed from other classes and although I > tried passing the instance of Directory into the new class (Item) but this > was causing real problems I don't understand why this would cause problems. Its certainly the normal approach to this kind of design scenario. > I noted that I couldn't put my argument for an instance of Directory after > **args (presumably because **args is used to mop up the remaining > arguments Correct, you would need to either treat directory as partrty of the **args stuff or pass it explicitly before them. > as a list) and when I put it before it messed up the reading of the list. And this I don't understand. > So my code now has this format- > >def GetCategories(self): >global platformCategories >platformCategories = self.GetPlatformsFile() >global typeCategories >typeCategories = self.GetTypesFile() > > The method above is within Directory and updates the two lists with the > values I need. (The methods called within GetCategories do the returning > of > the lists.) I'm afraid I don;t see anythoing here that should require a global, but I don't really undestand why passing a Directory instance around wasn't working. Can you explain that part of the problem a bit more? I guess I'd also be interested in why another class needs access to the data in Directory - what is it going to do to the data that Directory couldn't do for it? Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Setting a global variable on class initialisation
Hello again Sorry I didn't make the problem I'm working with all too clear. Basically I'm working with a class (Directory) that upon creation reads in a 3 files and from these files populates one main list. This bit is fine. The trickier bit was that I need to use two of the sublists (e.g the contents of two of the files) outwith the module. I didn't want to read the lists in again as I wanted the data to be set at the time the Directory class is created. The lists are accessed from other classes and although I tried passing the instance of Directory into the new class (Item) but this was causing real problems Item was trying to read in the lists: def __init__(self, filename, **args): I noted that I couldn't put my argument for an instance of Directory after **args (presumably because **args is used to mop up the remaining arguments as a list) and when I put it before it messed up the reading of the list. So my code now has this format- def GetCategories(self): #Assign the list values read in via platformCategories and typeCategories global platformCategories platformCategories = self.GetPlatformsFile() global typeCategories typeCategories = self.GetTypesFile() The method above is within Directory and updates the two lists with the values I need. (The methods called within GetCategories do the returning of the lists.) I think I've got a grasp on what was going wrong. ta Max RussellSenior Test Engineer BarcoBonnington Bond, 2 Anderson Place, Edinburgh EH6 5NP, UKTel + 44 (0) 131 472 5731 Fax + 44 (0) 131 472 4799www.barco.com[EMAIL PROTECTED]Unless indicated otherwise, the information contained in this message is privileged and confidential, and is intended only for the use of the addressee(s) named above and others who have been specifically authorized to receive it. If you are not the intended recipient, you are hereby notified that any dissemination, distribution or copying of this message and/or attachments is strictly prohibited. The company accepts no liability for any damage caused by any virus transmitted by this email. Furthermore, the company does not warrant a proper and complete transmission of this information, nor does it accept liability for any delays. If you have received this message in error, please contact the sender and delete the message. Thank you. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Setting a global variable on class initialisation
> 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. Hi Max, Could you give more context to this problem? I'm not sure we get it yet. *grin* > The lists need to would appear to need to be outwith the class I am > working with and global. Are the lists a part of the "state" of the instance? If not, why is this a part of the instance's initialization? I think we'd better wait on advice until we learn a little more about the situation. Good luck! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Setting a global variable on class initialisation
[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): 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 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 - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Setting a global variable on class initialisation
Max Russell wrote: > Hello- > > 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. What do you mean by class initialization? If you want to read the files and populate the lists when the class is defined, it might work to do this at the module level. Then the lists would be populated when the module is first imported: ## mymodule.py specialList = open('specialfile.txt').readlines() # Whatever file processing you need goes here class MyClass(object): # etc # do something with specialList If you want to read the files when an instance is initialized, then put the code in the class __init__() method. > The lists need to would appear to need to be outwith the class I am > working with and global. ?? What ?? Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Setting a global variable on class initialisation
Hello- 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? thanks Max RussellSenior Test Engineer BarcoBonnington Bond, 2 Anderson Place, Edinburgh EH6 5NP, UKTel + 44 (0) 131 472 5731 Fax + 44 (0) 131 472 4799www.barco.com[EMAIL PROTECTED]Unless indicated otherwise, the information contained in this message is privileged and confidential, and is intended only for the use of the addressee(s) named above and others who have been specifically authorized to receive it. If you are not the intended recipient, you are hereby notified that any dissemination, distribution or copying of this message and/or attachments is strictly prohibited. The company accepts no liability for any damage caused by any virus transmitted by this email. Furthermore, the company does not warrant a proper and complete transmission of this information, nor does it accept liability for any delays. If you have received this message in error, please contact the sender and delete the message. Thank you. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor