what *is* a class?

Uwe Mayer merkosh at hadiko.de
Mon Jun 17 16:39:00 CEST 2002


[This followup was posted to comp.lang.python and a copy was sent to the 
cited author.]

In article <aejn8r$pnh$1 at nntp9.atl.mindspring.net>, 
mgerrans at mindspring.com says...
> It creates and returns an instance (or object, if you prefer) of the class
> Group.   g is referring to that instance, so you haven't lost it, unless you
> assign g to something else.   Since you have a reference to the object you
> created (g), you don't need to "get ahold of it again" -- you still have a
> hold of (on?) it.

well, yes. and no.
if you want to create an object structure, mapping f.e. a binary file 
you have to somehow 'create' the structure. for this you don't have to 
read the file in. the problem was how to code this. assuming i have 
classes for BYTE, WORD, DWORD, LONG, FLOAT, etc. i could pass the root 
node (here: f.e. AVI()) a list of classes describing the header 
information:

AVI(BYTE, DWORD, BYTE, BYTE, BYTE, ...)

If you then build compound classes they perhaps consist of a length 
DWORD followed by 'length' number of bytes. it would be natural to 
compose this new structure LIST of more basic types (at least for the 
length DWORD, certainly not for the chain of data):

class LIST(...):
    def __init__(self, f):
        self.file = f
        self.length = BYTE().parse(f)
        ...

the last line creates a byte structure and reads it from file. i *have* 
to use a separate method call here, because imagine the AVI example from 
above having a string of 17 characters as an argument:

AVI(BYTE, DWORD, STR(len=17), ...)

STR(len=17) already instanciates a STR object while BYTE and DWORD only 
refer to classes.
In the above LIST declaration I could write:

self.length = BYTE()
self.length.parse(f)

... which looks a little humble to me.

 
> But if you insist on having a method to do it, you could define it like so:
> 
> class Group:
>    # ... other code.
>    def getRef(self):
>       return self
> 
> Then, instead of "g2 = g", you could do this:
> 
> g2 = g.getRef()

I guess when I tried that I wrote:

g2 = Group().getRef

instead of 

g2 = Group().getRef()
 
(ouch!)
which of course returned an "unbound method object" (IIRC)

> I can't think of any reason why you would want to do this though, unless you
> just need the extra typing practice and want to confuse someone who might be
> reading the code...

lol. :)
does the above explanation make sense now or is there a more common 
approach to what i'm doing?
 
> Perhaps I misunderstand your question and what you want is a factory.   That
> would be a method that creates and returns objects.   Of course, the
> constructor does this, but you might want a factory if you will instantiate
> different types of objects based on some runtime consideration.   If this is
> what you are after, then reply to that effect for more elaboration.

Yes. That sounds good, too. Depending on how you can define and create 
them.
I will be parsing arbitrary binary files. Up till now I'm just writing 
some base classes for later use. 

If you got some spare time I'd greatly appreciate it if you could either 
elaborate on factories or perhaps name a link with some related 
information to it.

Thanks
Ciao
Uwe



More information about the Python-list mailing list