Skip to content

Instantly share code, notes, and snippets.

@ktosiek
Created July 23, 2014 11:46
Show Gist options
  • Save ktosiek/f9a5c667a2cba74e8671 to your computer and use it in GitHub Desktop.
Save ktosiek/f9a5c667a2cba74e8671 to your computer and use it in GitHub Desktop.
Fun with object()
In [15]: class A(object):
....: pass
....:
In [16]: a = A()
In [17]: setattr(s, s, 10)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-17-a4e7f439dc1b> in <module>()
----> 1 setattr(s, s, 10)
TypeError: attribute name must be string, not 'object'
In [18]: A.__di
A.__dict__ A.__dictoffset__
In [18]: A.__dict
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-18-e6a51d23acd9> in <module>()
----> 1 A.__dict
AttributeError: type object 'A' has no attribute '__dict'
In [19]: A.__dict__
Out[19]:
<dictproxy {'__dict__': <attribute '__dict__' of 'A' objects>,
'__doc__': None,
'__module__': '__main__',
'__weakref__': <attribute '__weakref__' of 'A' objects>}>
In [20]: A.__dict__[s] = 10
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-20-5258824dbeef> in <module>()
----> 1 A.__dict__[s] = 10
TypeError: 'dictproxy' object does not support item assignment
In [21]: A.__dict__
Out[21]:
<dictproxy {'__dict__': <attribute '__dict__' of 'A' objects>,
'__doc__': None,
'__module__': '__main__',
'__weakref__': <attribute '__weakref__' of 'A' objects>}>
In [22]: dir(A.__dict__)
Out[22]:
['__class__',
'__cmp__',
'__contains__',
'__delattr__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getitem__',
'__gt__',
'__hash__',
'__init__',
'__iter__',
'__le__',
'__len__',
'__lt__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'copy',
'get',
'has_key',
'items',
'iteritems',
'iterkeys',
'itervalues',
'keys',
'values']
In [23]: setattr(s, s, 10)
KeyboardInterrupt
In [23]: type('asdf', (object, ), {s: 10})
Out[23]: __main__.asdf
In [24]: t = type('asdf', (object, ), {s: 10})
In [25]: t()
Out[25]: <__main__.asdf at 0x22c50d0>
In [26]: T = type('asdf', (object, ), {s: 10})
In [27]: t = T()
In [28]: getattr(t, s)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-28-75ea145dcc19> in <module>()
----> 1 getattr(t, s)
TypeError: getattr(): attribute name must be string
In [29]: dir(t)
Out[29]:
[<object at 0x7f56aed822b0>,
'__class__',
'__delattr__',
'__dict__',
'__doc__',
'__format__',
'__getattribute__',
'__hash__',
'__init__',
'__module__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'__weakref__']
In [30]: getattr(t, s)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-30-75ea145dcc19> in <module>()
----> 1 getattr(t, s)
TypeError: getattr(): attribute name must be string
In [31]: s
Out[31]: <object at 0x7f56aed822b0>
In [32]: dir(t)
Out[32]:
[<object at 0x7f56aed822b0>,
'__class__',
'__delattr__',
'__dict__',
'__doc__',
'__format__',
'__getattribute__',
'__hash__',
'__init__',
'__module__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'__weakref__']
In [33]: t.__dict__[s]
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-33-fb8942626012> in <module>()
----> 1 t.__dict__[s]
KeyError: <object object at 0x7f56aed822b0>
In [34]: t.__dict__.keys()
Out[34]: []
In [35]: t.__class__.__di
t.__class__.__dict__ t.__class__.__dictoffset__
In [35]: t.__class__.__dict__
Out[35]:
<dictproxy {<object at 0x7f56aed822b0>: 10,
'__dict__': <attribute '__dict__' of 'asdf' objects>,
'__doc__': None,
'__module__': '__main__',
'__weakref__': <attribute '__weakref__' of 'asdf' objects>}>
In [36]: t.__class__.__dict__[s]
Out[36]: 10
In [37]: t.__dict__ = {s: 20}
In [38]: t.__dict__
Out[38]: {<object at 0x7f56aed822b0>: 20}
In [39]: dir(t)
Out[39]:
[<object at 0x7f56aed822b0>,
'__class__',
'__delattr__',
'__dict__',
'__doc__',
'__format__',
'__getattribute__',
'__hash__',
'__init__',
'__module__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'__weakref__']
In [40]: ss = object()
In [41]: t.__dict__ = {s2: 20}
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-41-a33667b3f8c5> in <module>()
----> 1 t.__dict__ = {s2: 20}
NameError: name 's2' is not defined
In [42]: t.__dict__ = {ss: 20}
In [43]: dir(t)
Out[43]:
[<object at 0x7f56aed82280>,
<object at 0x7f56aed822b0>,
'__class__',
'__delattr__',
'__dict__',
'__doc__',
'__format__',
'__getattribute__',
'__hash__',
'__init__',
'__module__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'__weakref__']
In [44]: ss
Out[44]: <object at 0x7f56aed82280>
In [45]: getattr(t, ss)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-45-3c4add407dcb> in <module>()
----> 1 getattr(t, ss)
TypeError: getattr(): attribute name must be string
In [46]: class SS(st
%store stardcs.pfx static/ staticmethod stg_pg_settings str
In [46]: class SS(str):
....: def __hash__(self):
....: return 100
....:
In [47]: ss = SS()
In [48]: setattr(t, ss, 'asdf')
In [49]: ss2 = SS()
In [50]: getattr(t, ss2)
Out[50]: 'asdf'
In [51]: ss == ss2
Out[51]: True
In [52]: class SS(str):
def __hash__(self):
return 100
....: def __eq__(self, o):
....: return False
....:
In [53]: ss = SS(); ss2 = SS()
In [54]: ss == ss2
Out[54]: False
In [55]: ss2 = SS()
KeyboardInterrupt
In [55]: setattr(t, ss, 'xyz')
In [56]: getattr(t, ss)
Out[56]: 'xyz'
In [57]: getattr(t, ss2)
Out[57]: 'xyz'
In [58]:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment