Circular Class Logic
Paul McGuire
ptmcg at austin.rr.com
Thu Mar 15 15:25:33 EDT 2007
On Mar 15, 1:55 pm, half.ital... at gmail.com wrote:
> > class Disk(Folder):
> > def __init__(self,driveLetter):
> > super(Disk,self).__init__(driveLetter+":/")
>
> What is going on there? Is it just explicitly calling the super's
> init function?
What is going on is that Disk is being initialized with a single
character, such as 'D', and then calling the superclass with 'D:/',
the root folder on D. You're the one who said that Disk was a
subclass of Folder (sorry, you called it "Data"), so I tried to come
up with an example in which this made some kind of sense.
> How is that really different from this:
>
> class Disk(Folder):
> def __init__(self,driveLetter):
> Folder.Folder.__init__(self.path) # ??? Being that Folder is the
> superclass?
>
Where did self.path come from? Even though Folder is the superclass,
self.path doesn't exist until the Folder.__init__ method gets called.
This ain't C++ you know. And as I said above, we are taking a single
character drive letter, and calling the superclass init with the root
directory of that drive, so these are .
> I'd like to be able to use the shared Disk objects in any class that
> is a subclass of Data. Will the above method still make the Disk
> objects available in File.py? Also, what if the classes are in
> different modules.
>
> I'll try to experiment with it this afternoon. I'm using more
> information than just the drive letter. What if the user wants to
> find out the unc path to the share given the drive letter, or copy a
> file to the local disk that is not a system disk that has the most
> available space on it, or wants to mount or unmount a network drive.
>
Knock yourself out. I just hacked together these Folder/Disk classes
by way of a half-baked-but-working example. My point is that to
initialize a class level variable on class Foo, all that is needed is
to assign it in the defining module, that is:
class Foo:
pass
Foo.fooClassVar = "a class level variable"
Now any Foo or sub-Foo can access fooClassVar. The type of
fooClassVar can be anything you want, whether it is a Foo, subclass of
Foo or whatever, as long as it has been defined by the time you assign
fooClassVar.
-- Paul
More information about the Python-list
mailing list