Created
July 7, 2014 17:06
-
-
Save igr-santos/de21ae3ffbc38c808ba9 to your computer and use it in GitHub Desktop.
Comportamento interessante de se observar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Python 2.7.6 (default, Mar 22 2014, 22:59:56) | |
[GCC 4.8.2] on linux2 | |
Type "help", "copyright", "credits" or "license" for more information. | |
>>> class Foo(object): | |
... x = 9 | |
... def __init__(self, x): | |
... self.x = x | |
... def bar(self, y): | |
... return self.x + y | |
... | |
>>> | |
>>> foo = Foo(5) | |
>>> | |
>>> | |
>>> def bar(self, y): | |
... return self.x + y | |
... | |
>>> | |
>>> class Foo(object): | |
... x = 9 | |
... def __init__(self, x): | |
... self.x = x | |
... bar = bar | |
... | |
>>> | |
>>> foo = Foo(5) | |
>>> | |
>>> print bar(foo, 4) == 9 | |
True | |
>>> print bar(Foo, 0) == 9 | |
True | |
>>> | |
>>> print Foo.bar(foo, 4) == 9 | |
True | |
>>> print Foo.bar(Foo, 0) == 9 | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
TypeError: unbound method bar() must be called with Foo instance as first argument (got type instance instead) | |
>>> | |
>>> | |
>>> | |
>>> | |
>>> class Foo(object): | |
... __cache = [] | |
... def __init__(self, x): | |
... self.__cache.append(x) | |
... def add(self, x): | |
... self.__cache.append(x) | |
... return self | |
... | |
>>> | |
>>> foo_1 = Foo(10) | |
>>> foo_1.__cache | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
AttributeError: 'Foo' object has no attribute '__cache' | |
>>> class Foo(object): | |
... __cache = [] | |
... def __init__(self, x): | |
... self.__cache.append(x) | |
... def add(self, x): | |
... self.__cache.append(x) | |
... return self | |
... def get(self): | |
... return self.__cache | |
... | |
>>> | |
>>> | |
>>> | |
>>> | |
>>> foo_1 = Foo(10) | |
>>> foo_1.get() | |
[10] | |
>>> | |
>>> foo_1.add(5) | |
<__main__.Foo object at 0x7f15c99c2890> | |
>>> foo1_.get() | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
NameError: name 'foo1_' is not defined | |
>>> foo_1.get() | |
[10, 5] | |
>>> foo_1.add(6).add(8).get() | |
[10, 5, 6, 8] | |
>>> | |
>>> | |
>>> foo_1 = Foo(5) | |
>>> foo_1.get() | |
[10, 5, 6, 8, 5] | |
>>> | |
>>> foo_1.get() | |
[10, 5, 6, 8, 5] | |
>>> foo_1 = None | |
>>> | |
>>> foo_1 | |
>>> foo_1 = Foo(5) | |
>>> foo_1.get() | |
[10, 5, 6, 8, 5, 5] | |
>>> | |
>>> | |
>>> class Foo(object): | |
... x = [] | |
... def __init__(self, a): | |
... self.x.append(a) | |
... def add(self, a): | |
... self.x.append(a) | |
... return self | |
... def get(self): | |
... return self.x | |
... | |
>>> | |
>>> | |
>>> | |
>>> | |
>>> foo = Foo(0) | |
>>> | |
>>> foo.get() | |
[0] | |
>>> foo.add(3).add(4) | |
<__main__.Foo object at 0x7f15c99c2ad0> | |
>>> foo.get9) | |
File "<stdin>", line 1 | |
foo.get9) | |
^ | |
SyntaxError: invalid syntax | |
>>> foo.get() | |
[0, 3, 4] | |
>>> | |
>>> | |
>>> foo = Foo(8) | |
>>> foo.get9) | |
File "<stdin>", line 1 | |
foo.get9) | |
^ | |
SyntaxError: invalid syntax | |
>>> foo.get() | |
[0, 3, 4, 8] | |
>>> | |
>>> | |
>>> type(foo) | |
<class '__main__.Foo'> | |
>>> foo.add(2) | |
<__main__.Foo object at 0x7f15c99c2b90> | |
>>> | |
>>> | |
>>> | |
>>> | |
>>> del(foo) | |
>>> | |
>>> foo | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
NameError: name 'foo' is not defined | |
>>> foo = Foo(5) | |
>>> | |
>>> foo.get() | |
[0, 3, 4, 8, 2, 5] | |
>>> | |
>>> foo | |
<__main__.Foo object at 0x7f15c99c2bd0> | |
>>> | |
>>> foo.get() | |
[0, 3, 4, 8, 2, 5] | |
>>> | |
>>> | |
>>> foo = Foo(6) | |
>>> foo | |
<__main__.Foo object at 0x7f15c99c2ad0> | |
>>> | |
>>> foo.get() | |
[0, 3, 4, 8, 2, 5, 6] | |
>>> | |
>>> | |
>>> print cx | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
NameError: name 'cx' is not defined | |
>>> print foo.x | |
[0, 3, 4, 8, 2, 5, 6] | |
>>> | |
>>> | |
>>> | |
>>> class Foo(object): | |
... x = [] | |
... def __init__(self): | |
... self.x = [] | |
... def add(self, a): | |
... self.x.append(a) | |
... return self | |
... def get(self): | |
... return self.x | |
... | |
>>> | |
>>> | |
>>> | |
>>> t = Foo() | |
>>> t | |
<__main__.Foo object at 0x7f15c99c2d50> | |
>>> t.get( | |
... ) | |
[] | |
>>> t.add(1).add(4).add(5) | |
<__main__.Foo object at 0x7f15c99c2d50> | |
>>> t.get() | |
[1, 4, 5] | |
>>> | |
>>> | |
>>> | |
>>> a = Foo() | |
>>> a | |
<__main__.Foo object at 0x7f15c99c2e50> | |
>>> a.get() | |
[] | |
>>> a.add(4).add(2) | |
<__main__.Foo object at 0x7f15c99c2e50> | |
>>> a.get() | |
[4, 2] | |
>>> t.get() | |
[1, 4, 5] | |
>>> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment