Re: When is overriding __getattr__ is useful?

Discussion in 'Python' started by Albert Hopkins, Jan 7, 2013.

  1. On Mon, Jan 7, 2013, at 10:54 AM, Rodrick Brown wrote:
    > Can someone provide an example why one would want to override __getattr__
    > and __getattribute__ in a class?



    They're good for cases when you want to provide an "attribute-like"
    quality but you don't know the attribute in advance.

    For example, the xmlrpclib uses __getattr__ to "expose" XML-RPC methods
    over the wire when it doesn't necessarily know what methods are exposed
    by the service. This allows you do simply do

    >>> service.method(*args)


    And have the method "seem" like it's just a local method on an object.


    There are countless other examples. But that's just one that can be
    found in the standard library.
     
    Albert Hopkins, Jan 7, 2013
    #1
    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. Greg Brunet

    __getattr__ weirdness

    Greg Brunet, Aug 22, 2003, in forum: Python
    Replies:
    3
    Views:
    344
    Greg Brunet
    Aug 22, 2003
  2. Anand
    Replies:
    0
    Views:
    420
    Anand
    Dec 18, 2003
  3. Samuel Kleiner

    Operator overloading and __getattr__

    Samuel Kleiner, Dec 22, 2003, in forum: Python
    Replies:
    6
    Views:
    1,908
    Shalabh Chaturvedi
    Jan 13, 2004
  4. Holger Joukl

    py2.1->py2.3.3 __getattr__ confusion

    Holger Joukl, Jul 2, 2004, in forum: Python
    Replies:
    1
    Views:
    314
    Peter Otten
    Jul 2, 2004
  5. Rodrick Brown

    When is overriding __getattr__ is useful?

    Rodrick Brown, Jan 7, 2013, in forum: Python
    Replies:
    0
    Views:
    97
    Rodrick Brown
    Jan 7, 2013
Loading...

Share This Page