Il 12/06/2012 08:45, xor ha scritto:
http://web2py.com/books/default/chapter/29/3#Let%27s-count

On this link there is an example that tests the existence of a variable by just doing this: if not session.counter: I don't understand this. In python to test the existence of a variable I need to catch exceptions. How would this code work?

just try it and you'll understand :)

session.something works just like mydict.get('something') and in the case the key "something" does not exists it return None without raising any exception. To be more precise the session object belong to the Storage class...

https://github.com/web2py/web2py/blob/master/gluon/storage.py#L44

 """
    A Storage object is like a dictionary except `obj.foo` can be used
in addition to `obj['foo']`, and setting obj.foo = None deletes item foo.

>>> o = Storage(a=1)
>>> print o.a
        1

>>> o['a']
        1

>>> o.a = 2
>>> print o['a']
        2

>>> del o.a
>>> print o.a
        None
"""

Reply via email to