Skip to content

Instantly share code, notes, and snippets.

@shimizukawa
Created April 10, 2012 15:17
Show Gist options
  • Save shimizukawa/2352071 to your computer and use it in GitHub Desktop.
Save shimizukawa/2352071 to your computer and use it in GitHub Desktop.
エキPy読書会2nd #5 スクリーン
$ python
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class Foo(object):
... def __init__(self, name):
... self.name = name
...
...
...
>>> f = Foo('shimizukawa')
>>> f.__dict__
{'name': 'shimizukawa'}
>>> f.hoge = 'fuga'
>>> f.__dict__
{'name': 'shimizukawa', 'hoge': 'fuga'}
>>> class Foo(object):
... att = 10
...
>>> foo = Foo()
>>> foo.att
10
>>> foo.__dict__
{}
>>> Foo.__dict__
<dictproxy object at 0x7f241f89ad70>
>>> dict(Foo.__dict__)
{'__dict__': <attribute '__dict__' of 'Foo' objects>, '__module__': '__main__', '__weakref__': <attribute '__weakref__' of 'Foo' objects>, 'att': 10, '__doc__': None}
>>> foo.att = 20
>>> foo.__dict__
{'att': 20}
>>> foo.att
20
>>> del foo.att
>>> foo.att
10
>>> class Bar(object):
... def __getattr__(self, name):
... return name
...
>>> bar = Bar()
>>> bar.foo
'foo'
>>> bar.hoge
'hoge'
>>> bar.foo = 1
>>> bar.foo
1
>>> class Spam(object):
... pass
...
>>> def egg(self, name):
... return 'hello ' + name
...
>>> egg(1, 'ore')
'hello ore'
>>> spam = Spam()
>>> spam.foo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Spam' object has no attribute 'foo'
>>> Spam.foo = egg
>>> spam.foo('ore')
'hello ore'
>>> spam.bar = egg
>>> spam.bar('omae')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: egg() takes exactly 2 arguments (1 given)
>>> spam.bar(1, 'omae')
'hello omae'
>>> import types
>>> spam.bar = types.MethodType(egg, spam)
>>> spam.bar('omae')
'hello omae'
>>> spam2 = Spam()
>>> spam2.foo('You')
'hello You'
>>> spam2.bar('You')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Spam' object has no attribute 'bar'
>>> Spam.foo
<unbound method Spam.egg>
>>> spam.foo
<bound method Spam.egg of <__main__.Spam object at 0x7f241f8976d0>>
>>> foo
<__main__.Foo object at 0x7f241f897610>
>>> egg
<function egg at 0x17df0c8>
>>> egg.__get__
<method-wrapper '__get__' of function object at 0x17df0c8>
>>> dir(egg)
['__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__doc__', '__format__', '__get__', '__getattribute__', '__globals__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name']
>>> import pprint
>>> pprint.pprint(dir(egg))
['__call__',
'__class__',
'__closure__',
'__code__',
'__defaults__',
'__delattr__',
'__dict__',
'__doc__',
'__format__',
'__get__',
'__getattribute__',
'__globals__',
'__hash__',
'__init__',
'__module__',
'__name__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'func_closure',
'func_code',
'func_defaults',
'func_dict',
'func_doc',
'func_globals',
'func_name']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment