problem of types:

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

    #1

    problem of types:

    I do not understand why he is talking me about 'str', no str given!!!

    I have this:

    ---------------------------------------------

    def to_float(elt):
    if type(elt) is list:
    return map(to_float, elt)
    else:
    return float(elt)

    def Denombrement(A, b,c,type):
  • Fredrik Lundh

    #2
    Re: problem of types:

    Laurent wrote:
    [color=blue]
    > I do not understand why he is talking me about 'str', no str given!!!
    >
    > I have this:
    >
    > ---------------------------------------------
    >
    > def to_float(elt):
    > if type(elt) is list:[/color]

    this uses type
    [color=blue]
    > if __name__ == '__main__':
    > A = [[1,0],[0,1]]
    > b = [2,2]
    > c = [1,1]
    > type = 'min'[/color]

    and this sets type to a string.
    [color=blue]
    > if type(elt) is list:
    > TypeError: 'str' object is not callable
    >
    > Changing 'list' to 'type(list)' don't change anything!!!![/color]

    that's because the error says that you're trying to call a string,
    which is exactly what happens here.

    </F>



    Comment

    • Larry Bates

      #3
      Re: problem of types:

      Laurent wrote:[color=blue]
      > I do not understand why he is talking me about 'str', no str given!!!
      >
      > I have this:
      >
      > ---------------------------------------------
      >
      > def to_float(elt):
      > if type(elt) is list:
      > return map(to_float, elt)
      > else:
      > return float(elt)
      >
      > def Denombrement(A, b,c,type):
      > .
      > .
      > .
      > A = to_float(A)
      > b = to_float(b)
      > c = to_float(c)
      > .
      > .
      > .
      >
      > if __name__ == '__main__':
      > A = [[1,0],[0,1]]
      > b = [2,2]
      > c = [1,1]
      > type = 'min'
      >
      > Denombrement(A, b, c, type)
      >
      > -------------------------------------------
      >
      > And this error msg:
      > -------------------------
      > Traceback (most recent call last):
      > File "./Denombrement.py ", line 160, in ?
      > Denombrement(A, b, c, type)
      > File "./Denombrement.py ", line 31, in Denombrement
      > A = to_float(A)
      > File "./Denombrement.py ", line 10, in to_float
      > if type(elt) is list:
      > TypeError: 'str' object is not callable
      >
      > ------------------------------------------
      >
      > Changing 'list' to 'type(list)' don't change anything!!!!
      >[/color]

      Your string variable type is masking the built-in type
      function. You should avoid variables with names like
      type, list, str, tuple, ... (e.g. built-ins). As they
      will mask the built-ins from your program.

      -Larry Bates

      Comment

      Working...