Re: Know if a object member is a method

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

    Re: Know if a object member is a method

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Hi Luca,

    use type(something) .__name__ , e.g.
    >>def x():
    >>> pass
    >>class C:
    >>> pass
    >>c = C()
    >>type(x).__nam e__ == 'function'
    True
    >type(C).__name __ == 'classobj'
    True
    >type(c).__name __ == 'instance'
    True

    On Sep 1, 2008, at 10:43 AM, Luca wrote:
    Hi all.
    >
    I think this is a newbie question... what is the best method to know
    if a property of an object is a function?
    >
    I'm thinking something as
    >
    if type(obj.method Name)==???
    >
    Can someone help me?
    >
    --
    -- luca
    --

    >
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.7 (Darwin)

    iD8DBQFIu606cZ7 0OCIgLecRAhE6AJ 4r0GuHlWxXbLaYu olqpJStYPD+ggCg idKg
    qtgl+nbaKgH5Aoe lTu5WeJU=
    =W4eG
    -----END PGP SIGNATURE-----
  • Steven D'Aprano

    #2
    Re: Know if a object member is a method

    On Mon, 01 Sep 2008 10:52:10 +0200, Manuel Ebert wrote:

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1
    >
    Hi Luca,
    >
    use type(something) .__name__ , e.g.
    >
    >>def x():
    >>> pass
    >>class C:
    >>> pass
    >>c = C()
    >>type(x).__nam e__ == 'function'
    True
    >type(C).__name __ == 'classobj'
    True
    >type(c).__name __ == 'instance'
    True

    That's relatively fragile, since such names aren't reserved in any way.
    It's easy to fool a name comparison check with an accidental name
    collision:
    >>class function(object ): # not a reserved name
    .... pass
    ....
    >>x = function()
    >>type(x).__nam e__
    'function'
    >>x() # must be a function then...
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: 'function' object is not callable


    But not so easy to fool a type check:
    >>type(x) == new.function
    False

    Of course that's not bullet-proof either. I leave it as an exercise to
    discover how you might break that piece of code.



    --
    Steven

    Comment

    • Luca

      #3
      Re: Know if a object member is a method

      On Mon, Sep 1, 2008 at 11:35 AM, Steven D'Aprano
      <steven@remove. this.cybersourc e.com.auwrote:
      That's relatively fragile, since such names aren't reserved in any way.
      It's easy to fool a name comparison check with an accidental name
      collision:
      >
      >>>class function(object ): # not a reserved name
      ... pass
      ...
      >>>x = function()
      >>>type(x).__na me__
      'function'
      >>>x() # must be a function then...
      Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      TypeError: 'function' object is not callable
      >
      >
      But not so easy to fool a type check:
      >
      >>>type(x) == new.function
      False
      >
      Of course that's not bullet-proof either. I leave it as an exercise to
      discover how you might break that piece of code.
      >
      Ok, so...

      What is the best way to do this? The "most pythonic"?


      --
      -- luca

      Comment

      • Marc 'BlackJack' Rintsch

        #4
        Re: Know if a object member is a method

        On Mon, 01 Sep 2008 11:45:36 +0200, Luca wrote:
        >But not so easy to fool a type check:
        >>
        >>>>type(x) == new.function
        >False
        >>
        >Of course that's not bullet-proof either. I leave it as an exercise to
        >discover how you might break that piece of code.
        >>
        >>
        Ok, so...
        >
        What is the best way to do this? The "most pythonic"?
        The "most pythonic" might be not checking at all but simply *call* the
        object and deal with possible failures.

        What's your use case?

        Ciao,
        Marc 'BlackJack' Rintsch

        Comment

        • Steven D'Aprano

          #5
          Re: Know if a object member is a method

          On Mon, 01 Sep 2008 11:45:36 +0200, Luca asked about recognizing methods:
          What is the best way to do this? The "most pythonic"?
          That depends on why you are doing it, and what you want to do with the
          information once you've found it.

          If you are experimenting in the interactive interpreter, the easiest way
          is simply call the method and see what happens:

          obj.methodName( ) # call the method

          If it succeeds, then it is some sort of callable, a method or a function
          or something more unusual.

          If you fear side-effects, then use:

          callable(obj.me thodName)

          Alternatively, you can read the docs:

          help(obj)
          help(obj.method Name)

          If your aim is to write something like a debugger, profiler, or some
          other application that needs to inspect arbitrary objects and work out
          what they do, then you probably should be using:

          isinstance(obj. methodName, new.instancemet hod)

          But remember that not all callable attributes are instancemethods !

          There is no one right way of doing this.

          Hope this helps.



          --
          Steven

          Comment

          • Luca

            #6
            Re: Know if a object member is a method

            On Mon, Sep 1, 2008 at 2:18 PM, Steven D'Aprano
            <steve@remove-this-cybersource.com .auwrote:
            >
            If your aim is to write something like a debugger, profiler, or some
            other application that needs to inspect arbitrary objects and work out
            what they do, then you probably should be using:
            >
            isinstance(obj. methodName, new.instancemet hod)
            >
            But remember that not all callable attributes are instancemethods !
            >
            There is no one right way of doing this.
            >
            Hope this helps.
            Yes, this helps a lot. In facts I need to do something like a language parser.

            Thanks all.

            --
            -- luca

            Comment

            • Fredrik Lundh

              #7
              Re: Know if a object member is a method

              Luca wrote:
              Yes, this helps a lot. In facts I need to do something like a language parser.
              a *parser* that works on object structures created by executing a Python
              program?

              </F>

              Comment

              Working...