Skip to content

Instantly share code, notes, and snippets.

@elleryq
Created November 16, 2017 14:16
Show Gist options
  • Save elleryq/a23575a9732a697735ae99c90656a34b to your computer and use it in GitHub Desktop.
Save elleryq/a23575a9732a697735ae99c90656a34b to your computer and use it in GitHub Desktop.
python setattr example
#!/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