attribute error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • M.N.A.Smadi

    #1

    attribute error

    HI;

    I am having the following error. I am using someone else's code and
    all they are doing is pass an argv to a function then

    def execute_action( manager, argv):
    method_name = argv.pop(0).low er()


    and am getting this strange error.
    AttributeError: 'str' object has no attribute 'pop'

    am using Python 2.3.4 and am importing the following libraries:

    import sys, os, inspect
    from Asterisk import Manager, BaseException, Config
    import Asterisk.Util

    but i always thought that something like this will be standard stuff.
    Any ideas?

    thanks
    moe smadi
  • Steven D'Aprano

    #2
    Re: attribute error

    On Mon, 26 Sep 2005 18:28:56 -0400, M.N.A.Smadi wrote:
    [color=blue]
    > HI;
    >
    > I am having the following error. I am using someone else's code and
    > all they are doing is pass an argv to a function then
    >
    > def execute_action( manager, argv):
    > method_name = argv.pop(0).low er()
    >
    >
    > and am getting this strange error.
    > AttributeError: 'str' object has no attribute 'pop'[/color]

    Since you haven't told us what you have passed as argv, I will look into
    my crystal ball and use my psychic powers...

    Things are unclear... no, wait, I see a glimmer of light... are you using
    a single string, something like "this is a list of arguments"? The
    function is expecting a list of strings ["this", "is", "a", "list", "of",
    "arguments"].

    You may find the split() method useful for breaking up a single string
    into a list of strings.


    --
    Steven.

    Comment

    • Mike Meyer

      #3
      Re: attribute error

      "M.N.A.Smad i" <smadim2@grads. ece.mcmaster.ca > writes:
      [color=blue]
      > HI;
      >
      > I am having the following error. I am using someone else's code and
      > all they are doing is pass an argv to a function then
      >
      > def execute_action( manager, argv):
      > method_name = argv.pop(0).low er()
      >
      >
      > and am getting this strange error.
      > AttributeError: 'str' object has no attribute 'pop'
      >
      > am using Python 2.3.4 and am importing the following libraries:
      >
      > import sys, os, inspect
      > from Asterisk import Manager, BaseException, Config
      > import Asterisk.Util
      >
      > but i always thought that something like this will be standard stuff.
      > Any ideas?[/color]

      Yes - show us the rest of the code. execute_action is never called in
      the snippets you posted, and it's pretty clear that it's being invoked
      with the wrong thing as an argument. Can you provide a minimal working
      (well, executable) code sample that generates the error message you
      are getting?

      Oh yeah - post the full traceback as well.

      <mike

      --
      Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
      Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

      Comment

      • M.N.A.Smadi

        #4
        Re: attribute error

        HI;

        I am having the following error:

        AttributeError: 'str' object has no attribute 'pop'

        am using Python 2.3.4 and am importing the following libraries:

        import sys, os, inspect
        from Asterisk import Manager, BaseException, Config
        import Asterisk.Util

        The code being executed is:
        if command not in commands:
        raise ArgumentsError( 'invalid arguments.')



        if command == 'usage':
        return usage(argv[0], sys.stdout)

        manager = Manager.Manager (*Config.Config ().get_connecti on())

        if command == 'actions':
        show_actions()

        if command == 'help':
        if len(argv) < 3:
        raise ArgumentsError( 'please specify an action.')

        show_actions(ar gv[2])

        elif command == 'action':
        if len(argv) < 3:
        raise ArgumentsError( 'please specify an action.')

        try:
        execute_action( manager, argv[2:])
        except TypeError, e:
        print "Bad arguments specified. Help for %s:" % (argv[2],)
        show_actions(ar gv[2])

        elif command == 'command':
        execute_action( 'command', argv[2])

        def execute_action( manager, argv):
        method_name = argv.pop(0).low er()



        but i always thought that something like this will be standard stuff.
        Any ideas?


        Mike Meyer wrote:
        [color=blue]
        >"M.N.A.Smadi " <smadim2@grads. ece.mcmaster.ca > writes:
        >
        >
        >[color=green]
        >>HI;
        >>
        >>I am having the following error. I am using someone else's code and
        >>all they are doing is pass an argv to a function then
        >>
        >>def execute_action( manager, argv):
        >> method_name = argv.pop(0).low er()
        >>
        >>
        >>and am getting this strange error.
        >>AttributeErro r: 'str' object has no attribute 'pop'
        >>
        >>am using Python 2.3.4 and am importing the following libraries:
        >>
        >>import sys, os, inspect
        >>from Asterisk import Manager, BaseException, Config
        >>import Asterisk.Util
        >>
        >>but i always thought that something like this will be standard stuff.
        >>Any ideas?
        >>
        >>[/color]
        >
        >Yes - show us the rest of the code. execute_action is never called in
        >the snippets you posted, and it's pretty clear that it's being invoked
        >with the wrong thing as an argument. Can you provide a minimal working
        >(well, executable) code sample that generates the error message you
        >are getting?
        >
        >Oh yeah - post the full traceback as well.
        >
        > <mike
        >
        >
        >[/color]

        Comment

        • Mike Meyer

          #5
          Re: attribute error

          In <433C4402.60504 08@grads.ece.mc master.ca>, M.N.A.Smadi <smadim2@grads. ece.mcmaster.ca > typed:[color=blue]
          > HI;
          >
          > I am having the following error:
          >
          > AttributeError: 'str' object has no attribute 'pop'
          >
          > am using Python 2.3.4 and am importing the following libraries:
          >
          > import sys, os, inspect
          > from Asterisk import Manager, BaseException, Config
          > import Asterisk.Util
          >
          > The code being executed is:
          > if command not in commands:
          > raise ArgumentsError( 'invalid arguments.')
          >
          >
          >
          > if command == 'usage':
          > return usage(argv[0], sys.stdout)
          >
          > manager = Manager.Manager (*Config.Config ().get_connecti on())
          >
          > if command == 'actions':
          > show_actions()
          >
          > if command == 'help':
          > if len(argv) < 3:
          > raise ArgumentsError( 'please specify an action.')
          >
          > show_actions(ar gv[2])
          >
          > elif command == 'action':
          > if len(argv) < 3:
          > raise ArgumentsError( 'please specify an action.')
          >
          > try:
          > execute_action( manager, argv[2:])
          > except TypeError, e:
          > print "Bad arguments specified. Help for %s:" % (argv[2],)
          > show_actions(ar gv[2])
          >
          > elif command == 'command':
          > execute_action( 'command', argv[2])[/color]

          This should be execute_action( 'command', argv[2:]), with the ':' added.

          <mike
          [color=blue]
          > def execute_action( manager, argv):
          > method_name = argv.pop(0).low er()
          >
          >
          >
          > but i always thought that something like this will be standard stuff.
          > Any ideas?
          >
          >
          > Mike Meyer wrote:
          >[color=green]
          > >"M.N.A.Smadi " <smadim2@grads. ece.mcmaster.ca > writes:
          > >
          > >
          > >[color=darkred]
          > >>HI;
          > >>
          > >>I am having the following error. I am using someone else's code and
          > >>all they are doing is pass an argv to a function then
          > >>
          > >>def execute_action( manager, argv):
          > >> method_name = argv.pop(0).low er()
          > >>
          > >>
          > >>and am getting this strange error.
          > >>AttributeErro r: 'str' object has no attribute 'pop'
          > >>
          > >>am using Python 2.3.4 and am importing the following libraries:
          > >>
          > >>import sys, os, inspect
          > >>from Asterisk import Manager, BaseException, Config
          > >>import Asterisk.Util
          > >>
          > >>but i always thought that something like this will be standard stuff.
          > >>Any ideas?
          > >>
          > >>[/color]
          > >
          > >Yes - show us the rest of the code. execute_action is never called in
          > >the snippets you posted, and it's pretty clear that it's being invoked
          > >with the wrong thing as an argument. Can you provide a minimal working
          > >(well, executable) code sample that generates the error message you
          > >are getting?
          > >
          > >Oh yeah - post the full traceback as well.
          > >
          > > <mike
          > >
          > >
          > >[/color]
          >
          >[/color]

          --
          Mike Meyer <mwm@mired.or g> http://www.mired.org/consulting.html
          Independent Network/Unix/Perforce consultant, email for more information.

          Comment

          • M.N.A.Smadi

            #6
            Re: attribute error

            This has nothing to do with how the argument is passed. It is prob
            something wrong with str.pop in my python because when i run python and type
            import os
            import string
            x = '1 2 3'
            x.pop()

            i get the following error
            Traceback (most recent call last):
            File "<stdin>", line 1, in ?
            AttributeError: 'str' object has no attribute 'pop'


            Mike Meyer wrote:
            [color=blue]
            >In <433C4402.60504 08@grads.ece.mc master.ca>, M.N.A.Smadi <smadim2@grads. ece.mcmaster.ca > typed:
            >
            >[color=green]
            >>HI;
            >>
            >>I am having the following error:
            >>
            >>AttributeErro r: 'str' object has no attribute 'pop'
            >>
            >>am using Python 2.3.4 and am importing the following libraries:
            >>
            >>import sys, os, inspect
            >>from Asterisk import Manager, BaseException, Config
            >>import Asterisk.Util
            >>
            >>The code being executed is:
            >> if command not in commands:
            >> raise ArgumentsError( 'invalid arguments.')
            >>
            >>
            >>
            >> if command == 'usage':
            >> return usage(argv[0], sys.stdout)
            >>
            >> manager = Manager.Manager (*Config.Config ().get_connecti on())
            >>
            >> if command == 'actions':
            >> show_actions()
            >>
            >> if command == 'help':
            >> if len(argv) < 3:
            >> raise ArgumentsError( 'please specify an action.')
            >>
            >> show_actions(ar gv[2])
            >>
            >> elif command == 'action':
            >> if len(argv) < 3:
            >> raise ArgumentsError( 'please specify an action.')
            >>
            >> try:
            >> execute_action( manager, argv[2:])
            >> except TypeError, e:
            >> print "Bad arguments specified. Help for %s:" % (argv[2],)
            >> show_actions(ar gv[2])
            >>
            >> elif command == 'command':
            >> execute_action( 'command', argv[2])
            >>
            >>[/color]
            >
            >This should be execute_action( 'command', argv[2:]), with the ':' added.
            >
            > <mike
            >
            >
            >[color=green]
            >>def execute_action( manager, argv):
            >> method_name = argv.pop(0).low er()
            >>
            >>
            >>
            >>but i always thought that something like this will be standard stuff.
            >>Any ideas?
            >>
            >>
            >>Mike Meyer wrote:
            >>
            >>
            >>[color=darkred]
            >>>"M.N.A.Smadi " <smadim2@grads. ece.mcmaster.ca > writes:
            >>>
            >>>
            >>>
            >>>
            >>>
            >>>>HI;
            >>>>
            >>>>I am having the following error. I am using someone else's code and
            >>>>all they are doing is pass an argv to a function then
            >>>>
            >>>>def execute_action( manager, argv):
            >>>> method_name = argv.pop(0).low er()
            >>>>
            >>>>
            >>>>and am getting this strange error.
            >>>>AttributeEr ror: 'str' object has no attribute 'pop'
            >>>>
            >>>>am using Python 2.3.4 and am importing the following libraries:
            >>>>
            >>>>import sys, os, inspect
            >>>>
            >>>>
            >>>>from Asterisk import Manager, BaseException, Config
            >>>
            >>>
            >>>>import Asterisk.Util
            >>>>
            >>>>but i always thought that something like this will be standard stuff.
            >>>>Any ideas?
            >>>>
            >>>>
            >>>>
            >>>>
            >>>Yes - show us the rest of the code. execute_action is never called in
            >>>the snippets you posted, and it's pretty clear that it's being invoked
            >>>with the wrong thing as an argument. Can you provide a minimal working
            >>>(well, executable) code sample that generates the error message you
            >>>are getting?
            >>>
            >>>Oh yeah - post the full traceback as well.
            >>>
            >>> <mike
            >>>
            >>>
            >>>
            >>>
            >>>[/color]
            >>
            >>[/color]
            >
            >
            >[/color]

            Comment

            • Mike Meyer

              #7
              Re: attribute error

              In <433C473B.90509 01@grads.ece.mc master.ca>, M.N.A.Smadi <smadim2@grads. ece.mcmaster.ca > typed:[color=blue]
              > This has nothing to do with how the argument is passed. It is prob
              > something wrong with str.pop in my python because when i run python and type
              > import os
              > import string
              > x = '1 2 3'
              > x.pop()
              >
              > i get the following error
              > Traceback (most recent call last):
              > File "<stdin>", line 1, in ?
              > AttributeError: 'str' object has no attribute 'pop'[/color]

              The only thing wrong with str.pop is that you're trying to invoke
              it. The interpreter is telling you that string doesn't *have* a pop
              method. The interpreter is right. Strings are immutable, so "pop"
              doesn't make any sense for them.

              <mike
              [color=blue]
              >
              > Mike Meyer wrote:
              >[color=green]
              > >In <433C4402.60504 08@grads.ece.mc master.ca>, M.N.A.Smadi <smadim2@grads. ece.mcmaster.ca > typed:
              > >
              > >[color=darkred]
              > >>HI;
              > >>
              > >>I am having the following error:
              > >>
              > >>AttributeErro r: 'str' object has no attribute 'pop'
              > >>
              > >>am using Python 2.3.4 and am importing the following libraries:
              > >>
              > >>import sys, os, inspect
              > >>from Asterisk import Manager, BaseException, Config
              > >>import Asterisk.Util
              > >>
              > >>The code being executed is:
              > >> if command not in commands:
              > >> raise ArgumentsError( 'invalid arguments.')
              > >>
              > >>
              > >>
              > >> if command == 'usage':
              > >> return usage(argv[0], sys.stdout)
              > >>
              > >> manager = Manager.Manager (*Config.Config ().get_connecti on())
              > >>
              > >> if command == 'actions':
              > >> show_actions()
              > >>
              > >> if command == 'help':
              > >> if len(argv) < 3:
              > >> raise ArgumentsError( 'please specify an action.')
              > >>
              > >> show_actions(ar gv[2])
              > >>
              > >> elif command == 'action':
              > >> if len(argv) < 3:
              > >> raise ArgumentsError( 'please specify an action.')
              > >>
              > >> try:
              > >> execute_action( manager, argv[2:])
              > >> except TypeError, e:
              > >> print "Bad arguments specified. Help for %s:" % (argv[2],)
              > >> show_actions(ar gv[2])
              > >>
              > >> elif command == 'command':
              > >> execute_action( 'command', argv[2])
              > >>
              > >>[/color]
              > >
              > >This should be execute_action( 'command', argv[2:]), with the ':' added.
              > >
              > > <mike
              > >
              > >
              > >[color=darkred]
              > >>def execute_action( manager, argv):
              > >> method_name = argv.pop(0).low er()
              > >>
              > >>
              > >>
              > >>but i always thought that something like this will be standard stuff.
              > >>Any ideas?
              > >>
              > >>
              > >>Mike Meyer wrote:
              > >>
              > >>
              > >>
              > >>>"M.N.A.Smadi " <smadim2@grads. ece.mcmaster.ca > writes:
              > >>>
              > >>>
              > >>>
              > >>>
              > >>>
              > >>>>HI;
              > >>>>
              > >>>>I am having the following error. I am using someone else's code and
              > >>>>all they are doing is pass an argv to a function then
              > >>>>
              > >>>>def execute_action( manager, argv):
              > >>>> method_name = argv.pop(0).low er()
              > >>>>
              > >>>>
              > >>>>and am getting this strange error.
              > >>>>AttributeEr ror: 'str' object has no attribute 'pop'
              > >>>>
              > >>>>am using Python 2.3.4 and am importing the following libraries:
              > >>>>
              > >>>>import sys, os, inspect
              > >>>>
              > >>>>
              > >>>>from Asterisk import Manager, BaseException, Config
              > >>>
              > >>>
              > >>>>import Asterisk.Util
              > >>>>
              > >>>>but i always thought that something like this will be standard stuff.
              > >>>>Any ideas?
              > >>>>
              > >>>>
              > >>>>
              > >>>>
              > >>>Yes - show us the rest of the code. execute_action is never called in
              > >>>the snippets you posted, and it's pretty clear that it's being invoked
              > >>>with the wrong thing as an argument. Can you provide a minimal working
              > >>>(well, executable) code sample that generates the error message you
              > >>>are getting?
              > >>>
              > >>>Oh yeah - post the full traceback as well.
              > >>>
              > >>> <mike
              > >>>
              > >>>
              > >>>
              > >>>
              > >>>
              > >>
              > >>[/color]
              > >
              > >
              > >[/color]
              >
              >[/color]

              --
              Mike Meyer <mwm@mired.or g> http://www.mired.org/consulting.html
              Independent Network/Unix/Perforce consultant, email for more information.

              Comment

              • Steve Holden

                #8
                Re: attribute error

                Mike Meyer wrote:[color=blue]
                > In <433C473B.90509 01@grads.ece.mc master.ca>, M.N.A.Smadi <smadim2@grads. ece.mcmaster.ca > typed:
                >[color=green]
                >>This has nothing to do with how the argument is passed. It is prob
                >>something wrong with str.pop in my python because when i run python and type
                >>import os
                >>import string
                >>x = '1 2 3'
                >>x.pop()
                >>
                >>i get the following error
                >>Traceback (most recent call last):
                >> File "<stdin>", line 1, in ?
                >>AttributeErro r: 'str' object has no attribute 'pop'[/color]
                >
                >
                > The only thing wrong with str.pop is that you're trying to invoke
                > it. The interpreter is telling you that string doesn't *have* a pop
                > method. The interpreter is right. Strings are immutable, so "pop"
                > doesn't make any sense for them.
                >
                > <mike
                > [...][/color]

                Just to hammer the point home:

                Python 2.4.1 (#1, May 27 2005, 18:02:40)
                [GCC 3.3.3 (cygwin special)] on cygwin
                Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
                >>> x = '1 2 3'
                >>> x.pop()[/color][/color][/color]
                Traceback (most recent call last):
                File "<stdin>", line 1, in ?
                AttributeError: 'str' object has no attribute 'pop'[color=blue][color=green][color=darkred]
                >>> x = [1, 2, 3]
                >>> x.pop()[/color][/color][/color]
                3[color=blue][color=green][color=darkred]
                >>> x.pop()[/color][/color][/color]
                2[color=blue][color=green][color=darkred]
                >>> x[/color][/color][/color]
                [1][color=blue][color=green][color=darkred]
                >>>[/color][/color][/color]

                So if you genuinely have a string containing the values, split it onto a
                list first using something like

                x = x.split()

                regards
                Steve
                --
                Steve Holden +44 150 684 7255 +1 800 494 3119
                Holden Web LLC www.holdenweb.com
                PyCon TX 2006 www.pycon.org

                Comment

                • Steve Holden

                  #9
                  Re: attribute error

                  Mike Meyer wrote:[color=blue]
                  > In <433C473B.90509 01@grads.ece.mc master.ca>, M.N.A.Smadi <smadim2@grads. ece.mcmaster.ca > typed:
                  >[color=green]
                  >>This has nothing to do with how the argument is passed. It is prob
                  >>something wrong with str.pop in my python because when i run python and type
                  >>import os
                  >>import string
                  >>x = '1 2 3'
                  >>x.pop()
                  >>
                  >>i get the following error
                  >>Traceback (most recent call last):
                  >> File "<stdin>", line 1, in ?
                  >>AttributeErro r: 'str' object has no attribute 'pop'[/color]
                  >
                  >
                  > The only thing wrong with str.pop is that you're trying to invoke
                  > it. The interpreter is telling you that string doesn't *have* a pop
                  > method. The interpreter is right. Strings are immutable, so "pop"
                  > doesn't make any sense for them.
                  >
                  > <mike
                  > [...][/color]

                  Just to hammer the point home:

                  Python 2.4.1 (#1, May 27 2005, 18:02:40)
                  [GCC 3.3.3 (cygwin special)] on cygwin
                  Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
                  >>> x = '1 2 3'
                  >>> x.pop()[/color][/color][/color]
                  Traceback (most recent call last):
                  File "<stdin>", line 1, in ?
                  AttributeError: 'str' object has no attribute 'pop'[color=blue][color=green][color=darkred]
                  >>> x = [1, 2, 3]
                  >>> x.pop()[/color][/color][/color]
                  3[color=blue][color=green][color=darkred]
                  >>> x.pop()[/color][/color][/color]
                  2[color=blue][color=green][color=darkred]
                  >>> x[/color][/color][/color]
                  [1][color=blue][color=green][color=darkred]
                  >>>[/color][/color][/color]

                  So if you genuinely have a string containing the values, split it onto a
                  list first using something like

                  x = x.split()

                  regards
                  Steve
                  --
                  Steve Holden +44 150 684 7255 +1 800 494 3119
                  Holden Web LLC www.holdenweb.com
                  PyCon TX 2006 www.pycon.org

                  Comment

                  • Steven D'Aprano

                    #10
                    Re: attribute error

                    On Thu, 29 Sep 2005 15:57:47 -0400, M.N.A.Smadi wrote:
                    [color=blue]
                    > This has nothing to do with how the argument is passed. It is prob
                    > something wrong with str.pop in my python because when i run python and type
                    > import os
                    > import string
                    > x = '1 2 3'
                    > x.pop()
                    >
                    > i get the following error
                    > Traceback (most recent call last):
                    > File "<stdin>", line 1, in ?
                    > AttributeError: 'str' object has no attribute 'pop'[/color]


                    That's because strings don't have a pop method, just like the error says.
                    Did you read the error? Error messages frequently tell you what the error
                    is.

                    Lists have a pop method. You can't pop from a string. If you try
                    to pop from a string, you will get an error. You tried to pop from a
                    string, and it gave an error. The error told you what you did wrong:
                    you tried to pop from a string. Why are you surprised?

                    Go back to your code. Look at the variable that you are trying to pop
                    from, and notice that it is a string, just like the error message says.
                    Now search back through your code until you find the place where you
                    assign a string to that variable. Don't assign a string to it.

                    Problem fixed.

                    Like I said the first time I answered your question, I think your problem
                    is that you are passing in a single argument string like:

                    "this is a list of commands"

                    instead of a real list like:

                    ["this", "is", "a", "list", "of", "commands"]

                    As I also said the first time I answered this, you may find the split()
                    string method useful for creating that list. (Hint: if x is a string, you
                    call x.split() and get back a list.)

                    For future reference: any time you think you have found a bug in Python,
                    the chances are about 999,999 in a million that you've found a bug in your
                    code.


                    --
                    Steven.

                    Comment

                    Working...