use class factory to set required class variables?

Discussion in 'Python' started by Alan, Jan 26, 2011.

  1. Alan

    Alan Guest

    I have a class ``A`` that is intentionally incomplete:
    it has methods that refer to class variables that do not exist.
    The class ``A`` has several complicated methods, a number
    of which reference the "missing" class variables.
    Obviously, I do not directly use ``A``.

    I have a class factory ``f``` that subclasses ``A`` *only* in
    order to define the class variables.

    The motivation/problem:
    I did this because I need many versions of class ``A``,
    which differ only in the class variables, which are not
    known until run time.

    Q: On the face of it, did I pick a reasonable solution to
    my problem? If so is this a standard pattern? If not,
    can you mention a better approach?

    My solution is working for me, but the class ``A``
    is bugging me, because of the odd (to me) way in
    which it is incomplete. Obviously, I'm not a
    computer science type ...

    Thanks,
    Alan Isaac
    Alan, Jan 26, 2011
    #1
    1. Advertising

  2. Alan

    Alan Guest

    On Jan 26, 4:37 pm, Alan <> wrote:
    > I have a class factory ``f``` that subclasses ``A`` *only* in
    > order to define the class variables.


    I suppose it would be clearer to say that `f` *returns*
    subclasses of `A`. Hopefully that was clear ...

    Alan Isaac
    Alan, Jan 26, 2011
    #2
    1. Advertising

  3. On Wed, 26 Jan 2011 13:37:20 -0800, Alan wrote:

    > I have a class ``A`` that is intentionally incomplete: it has methods
    > that refer to class variables that do not exist.


    For the record, in Python it is usual to refer to "attributes" rather
    than "class variables" and "instance variables". In Python, classes are
    first-class objects (pun not intended) like ints, strings, floats etc.,
    and so a "class variable" would be a variable holding a class, just as a
    string variable would be a variable holding a string, an int variable
    holds an int, etc.


    > The class ``A`` has
    > several complicated methods, a number of which reference the "missing"
    > class variables. Obviously, I do not directly use ``A``.


    This is called an abstract class -- you need to subclass it to use it.


    > I have a class factory ``f``` that subclasses ``A`` *only* in order to
    > define the class variables.
    >
    > The motivation/problem:
    > I did this because I need many versions of class ``A``, which differ
    > only in the class variables, which are not known until run time.
    >
    > Q: On the face of it, did I pick a reasonable solution to my problem?
    > If so is this a standard pattern? If not, can you mention a better
    > approach?


    You have a class factory which subclasses an abstract class. Seems
    perfectly reasonable to me. More than reasonable -- it sounds like a
    *good* way of dealing with the problem.




    --
    Steven
    Steven D'Aprano, Jan 27, 2011
    #3
  4. Alan wrote:
    > I have a class ``A`` that is intentionally incomplete:
    > it has methods that refer to class variables that do not exist.
    > The class ``A`` has several complicated methods, a number
    > of which reference the "missing" class variables.
    > Obviously, I do not directly use ``A``.
    >
    > I have a class factory ``f``` that subclasses ``A`` *only* in
    > order to define the class variables.
    >
    > The motivation/problem:
    > I did this because I need many versions of class ``A``,
    > which differ only in the class variables, which are not
    > known until run time.
    >
    > Q: On the face of it, did I pick a reasonable solution to
    > my problem? If so is this a standard pattern? If not,
    > can you mention a better approach?
    >
    > My solution is working for me, but the class ``A``
    > is bugging me, because of the odd (to me) way in
    > which it is incomplete. Obviously, I'm not a
    > computer science type ...
    >
    > Thanks,
    > Alan Isaac
    >
    >

    Your design is perfectly fine. A is an abstract class, aka interface class.
    This is a very common pattern, that can solve many problems.

    However, and this is a personal advice, try to declare all the required
    attributes/methods in the abstract class, setting them to None or
    raising a NotImplementedError. That would help anyone, including you, to
    know what is required to define when subclassing A.

    class A:
    A_VALUE = None
    def aMethod(self):
    raise NotImplementedError()

    FYI, there is a python module that provide some feature to enhance your
    abstract classes: http://docs.python.org/library/abc.html. These are
    more advanced features, you may just ignore that module for now.

    JM
    Jean-Michel Pichavant, Jan 27, 2011
    #4
  5. Alan

    Bill Felton Guest

    On Jan 27, 2011, at 4:03 AM, Steven D'Aprano wrote:

    > On Wed, 26 Jan 2011 13:37:20 -0800, Alan wrote:
    >
    >> I have a class ``A`` that is intentionally incomplete: it has methods
    >> that refer to class variables that do not exist.

    >
    > For the record, in Python it is usual to refer to "attributes" rather
    > than "class variables" and "instance variables". In Python, classes are
    > first-class objects (pun not intended) like ints, strings, floats etc.,
    > and so a "class variable" would be a variable holding a class, just as a
    > string variable would be a variable holding a string, an int variable
    > holds an int, etc.
    >

    Being nit-picky, this is a slightly flawed justification of the terminology.
    In Smalltalk, classes are first class objects (pun similarly not intended). Yet in Smalltalk we refer to class variables and instance variables.
    These are not necessarily variables that 'hold' classes. In fact, all classes are instances, and so all variables hold instances...
    So there's something not quite right about your 'first class objects ... so ... would be a ...'.
    This may well be a standard Python convention, and it is certainly not objectionable. But it isn't well-justified on the basis of the 'first-class-ness' of classes as objects...

    Best regards,
    Bill
    Bill Felton, Jan 27, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Medi Montaseri
    Replies:
    17
    Views:
    851
    Medi Montaseri
    Sep 3, 2003
  2. Johan Bergman
    Replies:
    6
    Views:
    418
    Johan Bergman
    Jan 20, 2004
  3. Simon Elliott
    Replies:
    0
    Views:
    372
    Simon Elliott
    Jan 11, 2005
  4. C#
    Replies:
    4
    Views:
    395
  5. Alan
    Replies:
    5
    Views:
    311
Loading...

Share This Page