Skip to content

Instantly share code, notes, and snippets.

@shimizukawa
Created March 13, 2012 13:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shimizukawa/2028865 to your computer and use it in GitHub Desktop.
Save shimizukawa/2028865 to your computer and use it in GitHub Desktop.
エキPy読書会2nd #4 スクリーン
$ python
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> int
<type 'int'>
>>> type(10)
<type 'int'>
>>> dir(10)
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__',
'__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__',
'__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__',
'__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__',
'__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__',
'__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__',
'__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__',
'__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__',
'__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__',
'__xor__', 'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real']
>>> int.__str__
<slot wrapper '__str__' of 'int' objects>
>>> str(10)
'10'
>>> import types
>>> dir(types)
['BooleanType', 'BufferType', 'BuiltinFunctionType', 'BuiltinMethodType', 'ClassType',
'CodeType', 'ComplexType', 'DictProxyType', 'DictType', 'DictionaryType', 'EllipsisType',
'FileType', 'FloatType', 'FrameType', 'FunctionType', 'GeneratorType',
'GetSetDescriptorType', 'InstanceType', 'IntType', 'LambdaType', 'ListType', 'LongType',
'MemberDescriptorType', 'MethodType', 'ModuleType', 'NoneType', 'NotImplementedType',
'ObjectType', 'SliceType', 'StringType', 'StringTypes', 'TracebackType', 'TupleType',
'TypeType', 'UnboundMethodType', 'UnicodeType', 'XRangeType', '__builtins__', '__doc__',
'__file__', '__name__', '__package__']
>>> import UserList
>>> dir(UserList)
['UserList', '__builtins__', '__doc__', '__file__', '__name__', '__package__',
'collections']
>>> UserList.UserList
<class 'UserList.UserList'>
>>> class Aodag(UserList.UserList):
... pass
...
>>> a = Aodag()
>>> a
[]
>>> list
<type 'list'>
>>>
>>> class Dict(dict):
... def __getattr__(self, name):
... if name in self:
... return self[name]
... else:
... return super(Dict, self).__getattr__(name)
...
>>> d = Dict()
>>> d['foo'] = 1
>>> d.foo
1
>>> dir(dict)
['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__',
'__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__',
'__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems',
'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values',
'viewitems', 'viewkeys', 'viewvalues']
>>> dir({})
['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__',
'__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__',
'__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems',
'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values',
'viewitems', 'viewkeys', 'viewvalues']
>>> d['items'] = 2
>>> d.items
<built-in method items of Dict object at 0x027C7620>
>>>
>>> dict
<type 'dict'>
>>> dict()
{}
>>> super
<type 'super'>
>>> d
{'items': 2, 'foo': 1}
>>> super(Dict, d)
<super: <class 'Dict'>, <Dict object>>
>>> super(Dict, d).items
<built-in method items of Dict object at 0x027C7620>
>>> d.__getattr__
<bound method Dict.__getattr__ of {'items': 2, 'foo': 1}>
>>>
>>>
>>> super(Dict, d)
<super: <class 'Dict'>, <Dict object>>
>>> dict
<type 'dict'>
>>> d
{'items': 2, 'foo': 1}
>>>
>>> class BaseBase(object):
... def method(self):
... print "BaseBase"
...
>>> class Base1(BaseBase):
... pass
...
>>> class Base2(BaseBase):
... def method(self):
... print "Base2"
...
>>> class MyClass(Base1, Base2):
... pass
...
>>>
>>> m = MyClass()
>>> m.method()
Base2
>>> MyClass.__mro__
(<class '__main__.MyClass'>, <class '__main__.Base1'>, <class '__main__.Base2'>, <class
'__main__.BaseBase'>, <type 'object'>)
>>> super(Dict)
<super: <class 'Dict'>, NULL>
>>>
>>>
>>> dir(MyClass)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__',
'__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'method']
>>> MyClass.method
<unbound method MyClass.method>
>>> dir(MyClass.method)
['__call__', '__class__', '__cmp__', '__delattr__', '__doc__', '__format__', '__func__',
'__get__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__self__', '__setattr__', '__sizeof__', '__str__',
'__subclasshook__', 'im_class', 'im_func', 'im_self']
>>> MyClass.__mro__
(<class '__main__.MyClass'>, <class '__main__.Base1'>, <class '__main__.Base2'>, <class
'__main__.BaseBase'>, <type 'object'>)
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> class Spam(object):
... def __init__(self, arg):
... self.__arg = arg
... def getter(self):
... return self.__arg
... def setter(self, value):
... self.__arg = value
... arg = property(getter, setter)
...
>>> spam = Spam()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __init__() takes exactly 2 arguments (1 given)
>>> spam = Spam(10)
>>> spam.arg
10
>>> spam.arg = 1
>>> spam.arg
1
>>> import pdb
>>> spam._Spam__arg
1
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment