[Tutor] Setting a global variable on class initialisation
Alan Gauld
alan.gauld at freenet.co.uk
Fri Apr 28 12:28:55 CEST 2006
> 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.
More information about the Tutor
mailing list