problem with str()

Discussion in 'Python' started by 7stud, Mar 15, 2007.

  1. 7stud

    7stud Guest

    I can't get the str() method to work in the following code(the last
    line produces an error):

    ============
    class test:
    """class test"""
    def __init__(self):
    """I am init func!"""
    self.num = 10
    self.num2 = 20
    def someFunc(self):
    """I am someFunc in test!"""
    print "hello"


    obj = test()
    obj.someFunc()
    names = dir(obj)
    print names

    methodList = [str for str in names if callable(getattr(obj, str))]
    print methodList

    x = getattr(obj, methodList[0]).__doc__
    print x
    print type(x)
    print str(getattr(obj, methodList[0]).__doc__)
    ===========

    Here is the output:

    $ python test1.py
    hello
    ['__doc__', '__init__', '__module__', 'num', 'num2', 'someFunc']
    ['__init__', 'someFunc']
    I am init func!
    <type 'str'>
    Traceback (most recent call last):
    File "test1.py", line 23, in ?
    print str(getattr(obj, methodList[0]).__doc__)
    TypeError: 'str' object is not callable

    This is part of some code in Diving Into Python,Chapter 4. In case a
    function doesn't have a __doc__ string, and therefore __doc__ returns
    None, the code needs to convert each __doc__ to a string so that the
    result is guaranteed to be a string.
     
    7stud, Mar 15, 2007
    #1
    1. Advertising

  2. 7stud

    Matimus Guest

    Don't use built-ins as variable names. Your code will work if you
    change this:

    > methodList = [str for str in names if callable(getattr(obj, str))]


    to this:
    > methodList = [s for s in names if callable(getattr(obj, s))]
     
    Matimus, Mar 15, 2007
    #2
    1. Advertising

  3. 7stud

    Larry Bates Guest

    7stud wrote:
    > I can't get the str() method to work in the following code(the last
    > line produces an error):
    >
    > ============
    > class test:
    > """class test"""
    > def __init__(self):
    > """I am init func!"""
    > self.num = 10
    > self.num2 = 20
    > def someFunc(self):
    > """I am someFunc in test!"""
    > print "hello"
    >
    >
    > obj = test()
    > obj.someFunc()
    > names = dir(obj)
    > print names
    >
    > methodList = [str for str in names if callable(getattr(obj, str))]
    > print methodList
    >
    > x = getattr(obj, methodList[0]).__doc__
    > print x
    > print type(x)
    > print str(getattr(obj, methodList[0]).__doc__)
    > ===========
    >
    > Here is the output:
    >
    > $ python test1.py
    > hello
    > ['__doc__', '__init__', '__module__', 'num', 'num2', 'someFunc']
    > ['__init__', 'someFunc']
    > I am init func!
    > <type 'str'>
    > Traceback (most recent call last):
    > File "test1.py", line 23, in ?
    > print str(getattr(obj, methodList[0]).__doc__)
    > TypeError: 'str' object is not callable
    >
    > This is part of some code in Diving Into Python,Chapter 4. In case a
    > function doesn't have a __doc__ string, and therefore __doc__ returns
    > None, the code needs to convert each __doc__ to a string so that the
    > result is guaranteed to be a string.
    >


    You masked the built-in str method in your list comprehension.

    Try changing to:

    methodList = [s for s in names if callable(getattr(obj, str))]

    I'll bet it will work then.

    -Larry
     
    Larry Bates, Mar 15, 2007
    #3
  4. 7stud

    Guest

    On Mar 15, 2:49 pm, "7stud" <> wrote:
    > I can't get the str() method to work in the following code(the last
    > line produces an error):
    >
    > ============
    > class test:
    > """class test"""
    > def __init__(self):
    > """I am init func!"""
    > self.num = 10
    > self.num2 = 20
    > def someFunc(self):
    > """I am someFunc in test!"""
    > print "hello"
    >
    > obj = test()
    > obj.someFunc()
    > names = dir(obj)
    > print names
    >
    > methodList = [str for str in names if callable(getattr(obj, str))]
    > print methodList
    >
    > x = getattr(obj, methodList[0]).__doc__
    > print x
    > print type(x)
    > print str(getattr(obj, methodList[0]).__doc__)
    > ===========
    >
    > Here is the output:
    >
    > $ python test1.py
    > hello
    > ['__doc__', '__init__', '__module__', 'num', 'num2', 'someFunc']
    > ['__init__', 'someFunc']
    > I am init func!
    > <type 'str'>
    > Traceback (most recent call last):
    > File "test1.py", line 23, in ?
    > print str(getattr(obj, methodList[0]).__doc__)
    > TypeError: 'str' object is not callable
    >
    > This is part of some code in Diving Into Python,Chapter 4. In case a
    > function doesn't have a __doc__ string, and therefore __doc__ returns
    > None, the code needs to convert each __doc__ to a string so that the
    > result is guaranteed to be a string.


    Your string comprehension over wrote the str built-in method, turning
    it into a variable. If you just type "str" (without the quotes) into
    the interpreter, it'll spit out 'someFunc'. Thus, you cannot use str
    as the iterator in your code:

    methodList = [str for str in names if callable(getattr(obj, str))]

    instead, do something like this:

    methodList = [i for i in names if callable(getattr(obj, i))]

    Have fun!

    Mike
     
    , Mar 15, 2007
    #4
  5. 7stud

    Terry Reedy Guest

    "7stud" <> wrote in message
    news:...
    |I can't get the str() method to work in the following code(the last
    | line produces an error):

    If you 'print str' here

    | methodList = [str for str in names if callable(getattr(obj, str))]

    and again here, you will see the problem; you have reassigned the name
    'str' to something else by using it in the list comp. Hence the advice to
    never reuse
    builtin names unless you mean to lose access to the builtin object.

    |print str(getattr(obj, methodList[0]).__doc__)

    Here I presume you want the builtin function. Too bad... ;-)

    Terry Jan Reedy
     
    Terry Reedy, Mar 15, 2007
    #5
  6. 7stud

    7stud Guest

    Sheesh! You would think that after looking at every inch of the code
    for way too many hours, at some point that would have poked me in the
    eye.

    Thanks all.
     
    7stud, Mar 15, 2007
    #6
  7. 7stud

    Steve Holden Guest

    7stud wrote:
    > Sheesh! You would think that after looking at every inch of the code
    > for way too many hours, at some point that would have poked me in the
    > eye.
    >
    > Thanks all.
    >

    Get yourself a stuffed bear, and next time you have this kind of problem
    spend a few minutes explaining to the bear exactly how your program
    can't possibly be wrong. Works like a charm.

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    Blog of Note: http://holdenweb.blogspot.com
    See you at PyCon? http://us.pycon.org/TX2007
     
    Steve Holden, Mar 15, 2007
    #7
  8. En Thu, 15 Mar 2007 17:32:24 -0300, <> escribió:

    > methodList = [str for str in names if callable(getattr(obj, str))]
    >
    > instead, do something like this:
    >
    > methodList = [i for i in names if callable(getattr(obj, i))]


    The fact that a list comprehension "leaks" its variables into the
    containing scope is a bit weird.
    A generator expression doesn't:

    py> str
    <type 'str'>
    py> w = (str for str in range(10))
    py> w
    <generator object at 0x00AD7C38>
    py> str
    <type 'str'>
    py> w.next()
    0
    py> str
    <type 'str'>

    --
    Gabriel Genellina
     
    Gabriel Genellina, Mar 15, 2007
    #8
  9. 7stud

    Paul Rubin Guest

    writes:
    > methodList = [str for str in names if callable(getattr(obj, str))]
    >
    > instead, do something like this:
    >
    > methodList = [i for i in names if callable(getattr(obj, i))]


    or:

    methodList = list(str for str in names if callable(getattr(obj, str)))

    genexps, unlike listcomps, make a new scope for their index variable.
     
    Paul Rubin, Mar 15, 2007
    #9
  10. Steve Holden <> wrote:

    > 7stud wrote:
    > > Sheesh! You would think that after looking at every inch of the code
    > > for way too many hours, at some point that would have poked me in the
    > > eye.
    > >
    > > Thanks all.
    > >

    > Get yourself a stuffed bear, and next time you have this kind of problem
    > spend a few minutes explaining to the bear exactly how your program
    > can't possibly be wrong. Works like a charm.


    A rubber ducky works much better for that, btw -- more easily washable
    than a stuffed bear, for example.


    Alex
     
    Alex Martelli, Mar 16, 2007
    #10
  11. 7stud

    Steve Holden Guest

    Alex Martelli wrote:
    > Steve Holden <> wrote:
    >
    >> 7stud wrote:
    >>> Sheesh! You would think that after looking at every inch of the code
    >>> for way too many hours, at some point that would have poked me in the
    >>> eye.
    >>>
    >>> Thanks all.
    >>>

    >> Get yourself a stuffed bear, and next time you have this kind of problem
    >> spend a few minutes explaining to the bear exactly how your program
    >> can't possibly be wrong. Works like a charm.

    >
    > A rubber ducky works much better for that, btw -- more easily washable
    > than a stuffed bear, for example.
    >

    But stuffed bears are so much more knowledgeable about the minutiae of
    software design.

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    Blog of Note: http://holdenweb.blogspot.com
    See you at PyCon? http://us.pycon.org/TX2007
     
    Steve Holden, Mar 16, 2007
    #11
  12. 7stud

    7stud Guest

    On Mar 15, 5:31 pm, "Gabriel Genellina" <>
    wrote:
    > The fact that a list comprehension "leaks" its variables into the
    > containing scope is a bit weird.
    > A generator expression doesn't:
    >
    > py> str
    > <type 'str'>
    > py> w = (str for str in range(10))
    > py> w
    > <generator object at 0x00AD7C38>
    > py> str
    > <type 'str'>
    > py> w.next()
    > 0
    > py> str
    > <type 'str'>


    On Mar 15, 5:34 pm, Paul Rubin <http://> wrote:
    > or:
    >
    > methodList = list(str for str in names if callable(getattr(obj, str)))
    >
    > genexps, unlike listcomps, make a new scope for their index variable.



    Thanks.
     
    7stud, Mar 16, 2007
    #12
  13. Steve Holden <> wrote:
    ...
    > >> Get yourself a stuffed bear, and next time you have this kind of problem
    > >> spend a few minutes explaining to the bear exactly how your program
    > >> can't possibly be wrong. Works like a charm.

    > >
    > > A rubber ducky works much better for that, btw -- more easily washable
    > > than a stuffed bear, for example.
    > >

    > But stuffed bears are so much more knowledgeable about the minutiae of
    > software design.


    And yet, the key to Python's strength is duck typing -- if you can teach
    the duck to type, you've got it made.


    Alex
     
    Alex Martelli, Mar 16, 2007
    #13
  14. 7stud

    Steve Holden Guest

    Alex Martelli wrote:
    > Steve Holden <> wrote:
    > ...
    >>>> Get yourself a stuffed bear, and next time you have this kind of problem
    >>>> spend a few minutes explaining to the bear exactly how your program
    >>>> can't possibly be wrong. Works like a charm.
    >>> A rubber ducky works much better for that, btw -- more easily washable
    >>> than a stuffed bear, for example.
    >>>

    >> But stuffed bears are so much more knowledgeable about the minutiae of
    >> software design.

    >
    > And yet, the key to Python's strength is duck typing -- if you can teach
    > the duck to type, you've got it made.
    >
    >

    That's bearly a joke at all :)

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    Blog of Note: http://holdenweb.blogspot.com
    See you at PyCon? http://us.pycon.org/TX2007
     
    Steve Holden, Mar 16, 2007
    #14
    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. David
    Replies:
    2
    Views:
    484
    Thomas G. Marshall
    Aug 3, 2003
  2. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    650
    CBFalconer
    Apr 10, 2004
  3. Sullivan WxPyQtKinter

    It is fun.the result of str.lower(str())

    Sullivan WxPyQtKinter, Mar 7, 2006, in forum: Python
    Replies:
    5
    Views:
    344
    Tim Roberts
    Mar 9, 2006
  4. Stefan Ram

    str.equals(null) or str==null ?

    Stefan Ram, Jul 31, 2006, in forum: Java
    Replies:
    21
    Views:
    14,747
    Oliver Wong
    Aug 3, 2006
  5. maestro
    Replies:
    1
    Views:
    312
    Chris
    Aug 11, 2008
Loading...

Share This Page