CONSTRUCT - Adding Functionality to the Overall System

Discussion in 'Python' started by Ilias Lazaridis, Sep 17, 2006.

  1. I understand that I can use __metaclass__ to create a class which
    modifies the behaviour of another class.

    How can I add this metaclass to *all* classes in the system?

    (In ruby I would alter the "Class" class)

    ..

    http://lazaridis.com
     
    Ilias Lazaridis, Sep 17, 2006
    #1
    1. Advertising

  2. On 17 Sep 2006 09:22:16 -0700, Ilias Lazaridis <> wrote:
    > I understand that I can use __metaclass__ to create a class which
    > modifies the behaviour of another class.
    >
    > How can I add this metaclass to *all* classes in the system?
    >
    > (In ruby I would alter the "Class" class)


    This is a terrible idea. When my, Joe Modulewriter, released my
    library of AwesomeCoolClasses, i except my metaclass to be something
    particular and if it starts getting wonky and I do anything hackery on
    my end with the types, then the end-user's computer might 'splode!

    What I am trying to say, is its a terrible idea to go changing other
    peoples' classes at runtime without knowing the real affects of what
    is going on. If you are just going to shoot yourself in the foot, why
    should we even give you a gun?
     
    Calvin Spealman, Sep 17, 2006
    #2
    1. Advertising

  3. Ilias Lazaridis

    Georg Brandl Guest

    Ilias Lazaridis wrote:
    > I understand that I can use __metaclass__ to create a class which
    > modifies the behaviour of another class.
    >
    > How can I add this metaclass to *all* classes in the system?
    >
    > (In ruby I would alter the "Class" class)


    You'd have to set

    __metaclass__ = whatever

    at the top of each module whose classes are to get the new behavior.

    You can't alter classes which you don't control or create in your code.

    Georg
     
    Georg Brandl, Sep 17, 2006
    #3
  4. Ilias Lazaridis

    Damjan Guest


    >> I understand that I can use __metaclass__ to create a class which
    >> modifies the behaviour of another class.
    >>
    >> How can I add this metaclass to *all* classes in the system?
    >>
    >> (In ruby I would alter the "Class" class)

    >
    > You'd have to set
    >
    > __metaclass__ = whatever
    >
    > at the top of each module whose classes are to get the new behavior.


    I think '__metaclass__ = whatever' affects only the creation of classes that
    would otherwise be old-style classes?

    > You can't alter classes which you don't control or create in your code.


    I remeber I've seen an implementation of import_with_metaclass somewhere on
    IBM's developerworks. I didn't quite undersntad it though.



    --
    damjan
     
    Damjan, Sep 17, 2006
    #4
  5. Damjan wrote:
    > >> I understand that I can use __metaclass__ to create a class which
    > >> modifies the behaviour of another class.
    > >>
    > >> How can I add this metaclass to *all* classes in the system?
    > >>
    > >> (In ruby I would alter the "Class" class)

    > >
    > > You'd have to set
    > >
    > > __metaclass__ = whatever
    > >
    > > at the top of each module whose classes are to get the new behavior.

    >
    > I think '__metaclass__ = whatever' affects only the creation of classes that
    > would otherwise be old-style classes?


    It seems so:

    http://mail.python.org/pipermail/python-list/2003-June/166572.html

    > > You can't alter classes which you don't control or create in your code.

    >
    > I remeber I've seen an implementation of import_with_metaclass somewhere on
    > IBM's developerworks. I didn't quite undersntad it though.


    http://www-128.ibm.com/developerworks/linux/library/l-pymeta.html

    I am not so much interested in old-style, as is start production with
    python 2.4 (possibly even with python 2.5).

    ..

    --
    http://lazaridis.com
     
    Ilias Lazaridis, Sep 18, 2006
    #5
  6. Ilias Lazaridis

    Steve Holden Guest

    Ilias Lazaridis wrote:
    > Damjan wrote:
    >
    >>>>I understand that I can use __metaclass__ to create a class which
    >>>>modifies the behaviour of another class.
    >>>>
    >>>>How can I add this metaclass to *all* classes in the system?
    >>>>
    >>>>(In ruby I would alter the "Class" class)
    >>>
    >>>You'd have to set
    >>>
    >>>__metaclass__ = whatever
    >>>
    >>>at the top of each module whose classes are to get the new behavior.

    >>
    >>I think '__metaclass__ = whatever' affects only the creation of classes that
    >>would otherwise be old-style classes?

    >
    >
    > It seems so:
    >
    > http://mail.python.org/pipermail/python-list/2003-June/166572.html
    >
    >
    >>>You can't alter classes which you don't control or create in your code.

    >>
    >>I remeber I've seen an implementation of import_with_metaclass somewhere on
    >>IBM's developerworks. I didn't quite undersntad it though.

    >
    >
    > http://www-128.ibm.com/developerworks/linux/library/l-pymeta.html
    >
    > I am not so much interested in old-style, as is start production with
    > python 2.4 (possibly even with python 2.5).
    >

    The fact remains that you won't be able to affect the built-in classes
    such as int and str - they are hard-coded in C (for CPython, at least),
    and so their metaclass is also implied and cannot be changed.

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://holdenweb.blogspot.com
    Recent Ramblings http://del.icio.us/steve.holden
     
    Steve Holden, Sep 18, 2006
    #6
  7. Ilias Lazaridis

    Paul McGuire Guest

    "Calvin Spealman" <> wrote in message
    news:...
    > On 17 Sep 2006 09:22:16 -0700, Ilias Lazaridis <>
    > wrote:
    >> I understand that I can use __metaclass__ to create a class which
    >> modifies the behaviour of another class.
    >>
    >> How can I add this metaclass to *all* classes in the system?
    >>
    >> (In ruby I would alter the "Class" class)

    >
    > This is a terrible idea. When my, Joe Modulewriter, released my
    > library of AwesomeCoolClasses, i except my metaclass to be something
    > particular and if it starts getting wonky and I do anything hackery on
    > my end with the types, then the end-user's computer might 'splode!
    >
    > What I am trying to say, is its a terrible idea to go changing other
    > peoples' classes at runtime without knowing the real affects of what
    > is going on. If you are just going to shoot yourself in the foot, why
    > should we even give you a gun?


    Reminds me of when I was learning Smalltalk, and modified printOn (ST's
    equivalent of str()) of the root-of-all-classes object class. I don't
    remember the exact error I made, but it was a mess undoing it!

    -- Paul
     
    Paul McGuire, Sep 18, 2006
    #7
  8. Steve Holden wrote:
    > Ilias Lazaridis wrote:

    ....
    > > http://www-128.ibm.com/developerworks/linux/library/l-pymeta.html
    > >
    > > I am not so much interested in old-style, as is start production with
    > > python 2.4 (possibly even with python 2.5).
    > >

    >
    > The fact remains that you won't be able to affect the built-in classes
    > such as int and str - they are hard-coded in C (for CPython, at least),
    > and so their metaclass is also implied and cannot be changed.


    ....except in C.

    I assume the "root class" should be available/changeable in "C", too.

    Do I have to change the sources directly, or does python provide
    "C-level-extension/modication" mechanisms, which can be applied to
    core-level classes, too?

    Is the Python Object Model documented anywhere in a diagram, something
    similar to this?:

    http://case.lazaridis.com/wiki/RubyObjectModel

    ..
     
    Ilias Lazaridis, Sep 19, 2006
    #8
  9. On 18 Sep 2006 20:23:03 -0700, Ilias Lazaridis <> wrote:
    > Steve Holden wrote:
    > > Ilias Lazaridis wrote:

    > ...
    > > > http://www-128.ibm.com/developerworks/linux/library/l-pymeta.html
    > > >
    > > > I am not so much interested in old-style, as is start production with
    > > > python 2.4 (possibly even with python 2.5).
    > > >

    > >
    > > The fact remains that you won't be able to affect the built-in classes
    > > such as int and str - they are hard-coded in C (for CPython, at least),
    > > and so their metaclass is also implied and cannot be changed.

    >
    > ...except in C.
    >
    > I assume the "root class" should be available/changeable in "C", too.
    >
    > Do I have to change the sources directly, or does python provide
    > "C-level-extension/modication" mechanisms, which can be applied to
    > core-level classes, too?
    >
    > Is the Python Object Model documented anywhere in a diagram, something
    > similar to this?:
    >
    > http://case.lazaridis.com/wiki/RubyObjectModel


    What could you possibly want to do this for so badly that you would
    shun all alternatives and even resort to hacking up the runtime at the
    C-level to redefine core types in non-standard and unpredictable
    ways?! Seriously, please give a good reason for every doing this. I
    can't imagine there is any way it would justify all this in the face
    of just looking for an alternative. You aren't trying to use python,
    at this point, you are trying to fork it.
     
    Calvin Spealman, Sep 19, 2006
    #9
  10. Calvin Spealman wrote:
    > On 18 Sep 2006 20:23:03 -0700, Ilias Lazaridis <> wrote:
    > > Steve Holden wrote:
    > > > Ilias Lazaridis wrote:

    > > ...
    > > > > http://www-128.ibm.com/developerworks/linux/library/l-pymeta.html
    > > > >
    > > > > I am not so much interested in old-style, as is start production with
    > > > > python 2.4 (possibly even with python 2.5).
    > > >
    > > > The fact remains that you won't be able to affect the built-in classes
    > > > such as int and str - they are hard-coded in C (for CPython, at least),
    > > > and so their metaclass is also implied and cannot be changed.

    > >
    > > ...except in C.
    > >
    > > I assume the "root class" should be available/changeable in "C", too.
    > >
    > > Do I have to change the sources directly, or does python provide
    > > "C-level-extension/modication" mechanisms, which can be applied to
    > > core-level classes, too?
    > >
    > > Is the Python Object Model documented anywhere in a diagram, something
    > > similar to this?:
    > >
    > > http://case.lazaridis.com/wiki/RubyObjectModel

    >
    > What could you possibly want to do this for so badly that you would
    > shun all alternatives and even resort to hacking up the runtime at the
    > C-level to redefine core types in non-standard and unpredictable
    > ways?! Seriously, please give a good reason for every doing this. I
    > can't imagine there is any way it would justify all this in the face
    > of just looking for an alternative. You aren't trying to use python,
    > at this point, you are trying to fork it.


    http://case.lazaridis.com/wiki/Please#OffTopicPosts

    ..
     
    Ilias Lazaridis, Sep 19, 2006
    #10
  11. Ilias Lazaridis

    Georg Brandl Guest

    Damjan wrote:
    >>> I understand that I can use __metaclass__ to create a class which
    >>> modifies the behaviour of another class.
    >>>
    >>> How can I add this metaclass to *all* classes in the system?
    >>>
    >>> (In ruby I would alter the "Class" class)

    >>
    >> You'd have to set
    >>
    >> __metaclass__ = whatever
    >>
    >> at the top of each module whose classes are to get the new behavior.

    >
    > I think '__metaclass__ = whatever' affects only the creation of classes that
    > would otherwise be old-style classes?


    Wrong.

    If you set __metaclass__ = type, every class in that module will be new-style.

    If you set __metaclass__ = MyClass, and MyClass inherits from <type>, every
    class in that module will be new-style and have MyClass as a metaclass.

    The usual way to create new-style classes, inheriting from object or another
    new-style class, works because if no __metaclass__ is defined, the first
    base class's class is taken as the metaclass.

    Georg
     
    Georg Brandl, Sep 19, 2006
    #11
  12. >>
    >> I think '__metaclass__ = whatever' affects only the creation of
    >> classes that
    >> would otherwise be old-style classes?

    >
    > Wrong.
    >
    > If you set __metaclass__ = type, every class in that module will be
    > new-style.
    >
    > If you set __metaclass__ = MyClass, and MyClass inherits from <type>, every
    > class in that module will be new-style and have MyClass as a metaclass.
    >
    > The usual way to create new-style classes, inheriting from object or
    > another
    > new-style class, works because if no __metaclass__ is defined, the first
    > base class's class is taken as the metaclass.



    I was under that impression, too. But this behaves different (py2.4):


    ---- test.py ----
    class meta(type):
    def __new__(*args):
    print args
    return type(*args[1:])

    __metaclass__ = meta

    class OldStyle:
    pass

    class NewStyle(object):
    #__metaclass__ = meta

    pass



    ---- test.py ----

    deets$ python2.4 /tmp/test.py
    (<class '__main__.meta'>, 'OldStyle', (), {'__module__': '__main__'})
    deets$

    I was astonished to see that. Any explanation?

    Diez
     
    Diez B. Roggisch, Sep 19, 2006
    #12
  13. Ilias Lazaridis

    Damjan Guest

    >>> __metaclass__ = whatever
    >>>
    >>> at the top of each module whose classes are to get the new behavior.

    >>
    >> I think '__metaclass__ = whatever' affects only the creation of classes
    >> that would otherwise be old-style classes?

    >
    > Wrong.
    >
    > If you set __metaclass__ = type, every class in that module will be
    > new-style.


    >>> from UserDict import UserDict
    >>> type(UserDict)

    <type 'classobj'>
    >>> class A(UserDict):

    ... pass
    >>> type(A)

    <type 'classobj'>
    >>> __metaclass__ = type
    >>> class B(UserDict):

    ... pass
    >>> type(B)

    <type 'classobj'>

    It seems that NOT every class in my module will be a new style class,
    especially those that inherit from other old-style classes in other
    modules.



    --
    damjan
     
    Damjan, Sep 19, 2006
    #13
  14. Ilias Lazaridis

    Georg Brandl Guest

    Diez B. Roggisch wrote:
    >>>
    >>> I think '__metaclass__ = whatever' affects only the creation of
    >>> classes that
    >>> would otherwise be old-style classes?

    >>
    >> Wrong.
    >>
    >> If you set __metaclass__ = type, every class in that module will be
    >> new-style.
    >>
    >> If you set __metaclass__ = MyClass, and MyClass inherits from <type>, every
    >> class in that module will be new-style and have MyClass as a metaclass.
    >>
    >> The usual way to create new-style classes, inheriting from object or
    >> another
    >> new-style class, works because if no __metaclass__ is defined, the first
    >> base class's class is taken as the metaclass.

    >
    >
    > I was under that impression, too. But this behaves different (py2.4):


    [...]

    > deets$ python2.4 /tmp/test.py
    > (<class '__main__.meta'>, 'OldStyle', (), {'__module__': '__main__'})
    > deets$
    >
    > I was astonished to see that. Any explanation?


    Yes. I was wrong, the order of lookup is reversed. This first base class's
    class is used before the global __metaclass__.

    So what Damjan said is correct: global __metaclass__ is only usable for
    classes that don't have bases.

    Whether this is desirable is another question...

    Georg
     
    Georg Brandl, Sep 19, 2006
    #14
  15. Ilias Lazaridis wrote:
    > I understand that I can use __metaclass__ to create a class which
    > modifies the behaviour of another class.
    >
    > How can I add this metaclass to *all* classes in the system?
    >
    > (In ruby I would alter the "Class" class)


    I got confused from the discussion about __metaclass__.

    possibly a little more practical.

    I like to add a method "writeDebug(self, msg)" to all (or the most
    possible) classes in the system.

    How do I do this?

    * with new style classes
    * with old style classes

    > .
    >
    > http://lazaridis.com
     
    Ilias Lazaridis, Sep 19, 2006
    #15
  16. Ilias Lazaridis wrote:

    > Ilias Lazaridis wrote:
    >> I understand that I can use __metaclass__ to create a class which
    >> modifies the behaviour of another class.
    >>
    >> How can I add this metaclass to *all* classes in the system?
    >>
    >> (In ruby I would alter the "Class" class)

    >
    > I got confused from the discussion about __metaclass__.
    >
    > possibly a little more practical.
    >
    > I like to add a method "writeDebug(self, msg)" to all (or the most
    > possible) classes in the system.
    >
    > How do I do this?
    >
    > * with new style classes
    > * with old style classes


    OMFG. For asking the question (paraphrased) "why do you want to do this",
    Mr. Spealman received an

    http://case.lazaridis.com/wiki/Please#OffTopicPosts

    But now you do exactly that - give more details. Maybe this could teach you
    the lesson that the variance of input you get here to your questions is
    worth considering?

    But I've got the wicked feeling that this won't happen...

    Diez
     
    Diez B. Roggisch, Sep 19, 2006
    #16
  17. Ilias Lazaridis

    MonkeeSage Guest

    Ilias Lazaridis wrote:
    > How do I do this?


    It's easy:

    def writeDebug(msg):
    print "I do not debug things, I _evaluate_ with professionals on the
    industries! See ticket 547!\n" \
    "Oh yeah, and %s" % msg
    ....
    class Foo:
    writeDebug("how can I configure the interpreter for understand
    Klingon participals and noun-phrases? Must I rectify the standard
    dictionaries first?")

    Regards,
    Jordan
     
    MonkeeSage, Sep 19, 2006
    #17
  18. Diez B. Roggisch wrote:
    > Ilias Lazaridis wrote:
    > > Ilias Lazaridis wrote:
    > >> I understand that I can use __metaclass__ to create a class which
    > >> modifies the behaviour of another class.
    > >>
    > >> How can I add this metaclass to *all* classes in the system?
    > >>
    > >> (In ruby I would alter the "Class" class)

    > >
    > > I got confused from the discussion about __metaclass__.
    > >
    > > possibly a little more practical.
    > >
    > > I like to add a method "writeDebug(self, msg)" to all (or the most
    > > possible) classes in the system.
    > >
    > > How do I do this?
    > >
    > > * with new style classes
    > > * with old style classes

    >
    > OMFG. For asking the question (paraphrased) "why do you want to do this",
    > Mr. Spealman received an
    >
    > http://case.lazaridis.com/wiki/Please#OffTopicPosts
    >
    > But now you do exactly that - give more details. Maybe this could teach you
    > the lesson that the variance of input you get here to your questions is
    > worth considering?
    >
    > But I've got the wicked feeling that this won't happen...


    I've not given more details, but less.

    Just simplified my request.

    ..
     
    Ilias Lazaridis, Sep 19, 2006
    #18
  19. MonkeeSage wrote:
    > Ilias Lazaridis wrote:
    > > How do I do this?

    >
    > It's easy:
    >
    > def writeDebug(msg):
    > print "I do not debug things, I _evaluate_ with professionals on the
    > industries! See ticket 547!\n" \
    > "Oh yeah, and %s" % msg


    where do I place this function...

    > ...
    > class Foo:
    > writeDebug("how can I configure the interpreter for understand
    > Klingon participals and noun-phrases? Must I rectify the standard
    > dictionaries first?")


    ....thus it becomes available within class "Foo" and all other Classes?

    Something like a central import?

    ..
     
    Ilias Lazaridis, Sep 19, 2006
    #19
  20. Ilias Lazaridis

    MonkeeSage Guest

    Ilias Lazaridis wrote:
    > where do I place this function...


    The place where you want it to be.

    > ...thus it becomes available within class "Foo" and all other Classes?


    Anything defined in the top-level (i.e., the sys.modules['__main__']
    namespace) is accessible in every scope...but I assume you already know
    that.

    You could also use a super-duper super class from which to derive all
    your other classes, and add/replace any methods you want there:

    class lazaridis(object):
    def evaluate(self, community):
    if 1 is 1:
    print "Evaluating %s... Result: failed!" % community

    class Foo(lazaridis, str):
    pass

    f=Foo('This has been a test of the lazaridis evaluation system')
    f.evaluate('Python')
    f.evaluate('Ruby')
    f.evaluate('Java')
    print f

    > Something like a central import?


    That would probably be the most logical thing to do.

    But again, I assume you already know all this, so why are you asking?
    Is this part of the evaluation process?

    Regards,
    Jordan
     
    MonkeeSage, Sep 19, 2006
    #20
    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. Paul Ritchie

    Best overall reporting package?

    Paul Ritchie, Aug 14, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    2,783
    dotnetdev18
    Oct 27, 2008
  2. Mike Moore [MSFT]

    RE: 'Overall' Editing DataGrids

    Mike Moore [MSFT], Nov 11, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    494
    Mike Moore [MSFT]
    Nov 12, 2003
  3. Sebi

    Overall editing DataGrids

    Sebi, Nov 12, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    344
  4. Alan Silver
    Replies:
    20
    Views:
    2,165
    Alan Silver
    Nov 1, 2005
  5. Mukesh
    Replies:
    4
    Views:
    626
    Paul N
    Mar 26, 2010
Loading...

Share This Page