getattr

Discussion in 'Python' started by Srikanth Mandava, Feb 19, 2004.

  1. Hi,

    How can getattr() be used to call a constructor method?

    Thanks
    Srikanth
     
    Srikanth Mandava, Feb 19, 2004
    #1
    1. Advertising

  2. Srikanth Mandava <> writes:

    > How can getattr() be used to call a constructor method?


    Huh?

    Cheers,
    mwh

    --
    About the use of language: it is impossible to sharpen a
    pencil with a blunt axe. It is equally vain to try to do
    it with ten blunt axes instead.
    -- E.W.Dijkstra, 18th June 1975. Perl did not exist at the time.
     
    Michael Hudson, Feb 19, 2004
    #2
    1. Advertising

  3. Srikanth Mandava

    Peter Hansen Guest

    > Srikanth Mandava wrote:
    >
    > How can getattr() be used to call a constructor method?


    First thing to understand is that "constructor method" is an unclear
    concept in Python. *Old-style* classes don't have constructors, they
    have the __init__ initializer method. When you call it, you don't
    get back an instance, since it returns None.

    New-style classes have a different approach, and real constructors
    are available. (As I understand it... heck, I haven't even used
    them yet! :)

    Are you perhaps thinking of using getattr() to get a reference to the
    __init__ method of something, and then calling it? Why would you
    want to use getattr() for that when you can just as for the method
    reference directly? But more to the point: that won't work, since
    __init__ is not a constructor. What you would really be looking for
    in this case is the __class__ property, which is callable and which
    is probably the closest thing to the "constructor" concept that you
    will find here.

    If none of this answers the question, try again with more detail
    and maybe an example of what you were hoping to do, even if it
    doesn't actually work or isn't syntactically correct Python, and
    then we'll have a clue what you mean.

    -Peter
     
    Peter Hansen, Feb 19, 2004
    #3
    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. daishi
    Replies:
    0
    Views:
    567
    daishi
    Jul 25, 2003
  2. Brian Roberts

    Confused about hasattr/getattr/namespaces

    Brian Roberts, Feb 29, 2004, in forum: Python
    Replies:
    2
    Views:
    309
    Bob Ippolito
    Feb 29, 2004
  3. SimonVC

    getattr() in default namespace.

    SimonVC, Apr 8, 2004, in forum: Python
    Replies:
    3
    Views:
    911
    SimonVC
    Apr 9, 2004
  4. Thomas Rast

    getattr() woes

    Thomas Rast, Dec 29, 2004, in forum: Python
    Replies:
    4
    Views:
    637
    Kamilche
    Dec 31, 2004
  5. Johnny
    Replies:
    3
    Views:
    470
    Robert Kern
    Aug 23, 2005
Loading...

Share This Page