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
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) |
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
In [1]: class Foo(object): | |
...: for i in range(3): | |
...: locals()["x" + str(i)] = i | |
...: | |
In [2]: Foo.x2 | |
Out[2]: 2 |
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
In [98]: class Thing(object): | |
....: import json | |
....: | |
In [99]: Thing.json | |
Out[99]: <module 'json' from '.../lib/python3.5/json/__init__.py'> |
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
In [91]: class Thing(object): | |
print(locals()) | |
....: | |
{'__qualname__': 'Thing', '__module__': '__main__'} |
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
class Other(object): | |
def test(self): | |
print("test") | |
o = Other() | |
o.test() |
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
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 |
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
# 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 |
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
In [17]: class Butter(object): | |
....: if input("salt?") == "yes": | |
....: salt = True | |
....: else: | |
....: salt = False | |
....: | |
salt?yes | |
In [18]: Butter.salt | |
Out[18]: True |
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
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> |
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
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 |