views:

113

answers:

2

Hi there,

Python Shell - shell.appspot.com is acting weird? or am I missing something?

Google App Engine/1.3.0
Python 2.5.2 (r252:60911, Apr  7 2009, 17:42:26) 
[GCC 4.1.0]

>>> mycolors = ['red','green','blue']
>>> mycolors.append('black')
>>> print mycolors
['red', 'green', 'blue']

But the below result is expected

['red', 'green', 'blue', 'black']

And also same with the dictionary data type.

Thanks,

Abhinay

+1  A: 

I get similar problems so I would say there is something odd going on.

>>> a = 2
>>> a += 3
>>> a
5
>>> b = [2]
>>> b += [3]
>>> b
[2]
>>> [2] + [3]
[2, 3]
>>> class Dave: pass
>>> d = Dave()
>>> d
<__main__.Dave instance at 0x6df2d063e08a98e8>
>>> d.a = 1
>>> d.a
Traceback (most recent call last):
  File "/base/data/home/apps/shell/1.335852500710379686/shell.py", line 267, in get
    exec compiled in statement_module.__dict__
  File "<string>", line 1, in <module>
AttributeError: Dave instance has no attribute 'a'

It appears this issue has already been reported: Issue 29: Shell - entities are immutable in the shell. Unfortunately, there's response to the logged issue.

Dave Webb
just tried it myself... something's wrong.
jldupont
Does anyone know why? Go(d)ogle knows it!
abhiomkar
+4  A: 

Short Answer

That is a known bug. Short answer:

  • Include everything on one line: mycolors.append('black'); print mycolors
  • Use my free software tool, App Engine Console. My code is derived from the shell and I have fixed this bug.

Long answer

The bug involves the way that state is stored in between every command you type. Web requests are stateless and request/response only; however the shell (and my console app) is supposed to feel like a stream of consciousness as it is at the traditional Python prompt.

The implementation is roughly this:

  1. Get a request from a browser which includes a line to execute
  2. Pull up the specific session which the browser is "in". Basically that is a module similar to __main__ with some variable bindings.
  3. Execute the given line of code in that module's context
  4. Save state by looping through all variable bindings in the context and...
    • If the variable is hitherto unseen, store its name and value in the datastore
    • If not, ignore it. Here is the bug. You should actually check whether the variable has changed.
jhs
abhiomkar
You're welcome! The console is not quite new, nor is it perfect, but I use it myself regularly. I am planning a refactor, moving to Django. Feel free to leave feedback with ideas!
jhs