newb: object factory, and iteration

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris

    newb: object factory, and iteration

    I'm developing a thing that gathers data as follows:
    1. For all files of a certain name:
    I. for all lines in each of those files:
    i. split the /-separated values in each of those lines,
    and stuff them into a list (or dict or whatever)

    .... and this is for a hundred or so such files of that name, with each
    file having a hundred or so of such lines, with each line having 10 or
    so of such /-sep'd values.

    I thought that an instructive way of doing this would be to have a
    class describing each of these files and their contents, and create a
    new instance of this class for each file. As I'm writing this, it
    occurs to me that I could stick to a numbered naming convention (e.g.,
    df1, df2, etc) for when I need to access this data later in the
    program. But is there a way for me to list all instances of a certain
    class? I.e., if the classname were FileInfo, and I had hundred
    objects of that class, could I somehow loop:

    for all objects created from FileInfo
    do some stuff

    TIA for any help. I'll get used to Python sooner or later =)

    -cjl
  • Jeremy Yallop

    #2
    Re: newb: object factory, and iteration

    Chris wrote:[color=blue]
    > But is there a way for me to list all instances of a certain
    > class?[/color]

    I don't think there is, in general. If, as in your example, you can
    modify the __init__ method of the class, you could add weak references
    to created instances to a class member list. It would be even nicer,
    though, to make a metaclass, instances of which maintain (weak)
    references to instances of themselves.

    Jeremy.

    Comment

    Working...