Skip to content

Instantly share code, notes, and snippets.

View makmanalp's full-sized avatar

Mehmet Ali "Mali" Akmanalp makmanalp

View GitHub Profile
@makmanalp
makmanalp / test.py
Created June 7, 2016 02:27
Code in the body of a class - 12
In [1]: class Foo(object):
...: for i in range(3):
...: setattr(Foo, "x"+str(i), i)
...:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-1-bcc534efdfb8> in <module>()
----> 1 class Foo(object):
2 for i in range(3):
3 setattr(Foo, "x"+str(i), i)
@makmanalp
makmanalp / test.py
Created June 7, 2016 02:23
Code in the body of a class - 11
In [1]: class Foo(object):
...: for i in range(3):
...: locals()["x" + str(i)] = i
...:
In [2]: Foo.x2
Out[2]: 2
@makmanalp
makmanalp / thing.py
Created June 6, 2016 05:34
Code in the body of a class - 10
In [98]: class Thing(object):
....: import json
....:
In [99]: Thing.json
Out[99]: <module 'json' from '.../lib/python3.5/json/__init__.py'>
@makmanalp
makmanalp / test.py
Created June 6, 2016 05:26
Code in the body of a class - 9
In [91]: class Thing(object):
print(locals())
....:
{'__qualname__': 'Thing', '__module__': '__main__'}
@makmanalp
makmanalp / other.py
Created June 6, 2016 05:08
Code in the body of a class - 8
class Other(object):
def test(self):
print("test")
o = Other()
o.test()
@makmanalp
makmanalp / test.py
Created June 6, 2016 04:25
Code in the body of a class - 8
In [83]: class Foo(object):
for i in range(3):
setattr(Foo, "x"+str(i), i)
....:
In [84]: Foo.x0
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-84-e9bd27bad8bd> in <module>()
----> 1 Foo.x0
@makmanalp
makmanalp / test.py
Last active June 6, 2016 04:54
Code in the body of a class - 7
# If you legitimately need to get in there before class definition, say because you're generating something that already has a metaclass
# that needs to run (e.g. dynamically generating Django models), you could use type(), like this:
def class_factory_1(num_attrs):
attrs = {"var_{}".format(i): i for i in range(num_attrs)}
return type("Klass", (SomeBaseClass,), attrs)
# Otherwise you could use a regular old class definition for a slightly less smartypants solution
def class_factory_2(num_attrs):
class Inner(object):
pass
@makmanalp
makmanalp / test.py
Created June 6, 2016 01:54
Code in the body of a class - 6
In [17]: class Butter(object):
....: if input("salt?") == "yes":
....: salt = True
....: else:
....: salt = False
....:
salt?yes
In [18]: Butter.salt
Out[18]: True
@makmanalp
makmanalp / test.py
Last active June 6, 2016 01:14
Code in the body of a class - 5
In [8]: class Blah(object):
....: class Blah2(object):
....: pass
....:
In [9]: Blah.Blah2
Out[9]: __main__.Blah.Blah2
In [10]: Blah.Blah2()
Out[10]: <__main__.Blah.Blah2 at 0x10c77a630>
@makmanalp
makmanalp / test.py
Created June 6, 2016 01:05
Code in the body of a class - 4
In [7]: class Qux(object):
...: raise ValueError
...:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-7-8d9958c73a44> in <module>()
----> 1 class Qux(object):
2 raise ValueError
3