[ create a new paste ] login | about

Link: http://codepad.org/CaZxRDbV    [ raw code | output | fork ]

aaronla - Python, pasted on Feb 21:
1
2
3
4
5
6
7
8
9
10
11
12
13
def def_struct(name, *fields):
    c = "class %s(object):\n" % name
    c+= "  def __init__(self" + ''.join(',%s'%f for f in fields) + "):\n"
    c+= (''.join(
        "    self.%s=%s\n"%(f,f) for f in fields) if fields else 
        "    pass\n")
    exec(c, globals(), globals())

def_struct("Thing", "a", "b")

t1 = Thing(1, 2)
print "Thing.a =", t1.a
print "Thing.b =", t1.b


Output:
1
2
Thing.a = 1
Thing.b = 2


Create a new paste based on this one


Comments: