Created
November 16, 2017 14:16
-
-
Save elleryq/a23575a9732a697735ae99c90656a34b to your computer and use it in GitHub Desktop.
python setattr example
This file contains hidden or 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
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| class employee: | |
| def __init__(self, name): | |
| self.name = name | |
| def __str__(self): | |
| return "{clsname}(name={name})".format( | |
| clsname=self.__class__.__name__, | |
| name=self.name) | |
| class company(): | |
| def __init__(self): | |
| for name in ('a', 'b', 'c', 'd', 'e', 'f'): | |
| setattr(self, name, employee(name)) | |
| t = company() | |
| print(t.a) | |
| print(t.b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment