''' Created on Jan 8, 2016 @author: Brett Paufler Copyright Brett Paufler Can Kill There are some nice class implementations here ''' ''' from collections import namedtuple #For in x = [y + x for x in ['ab', 'cd'] for y in x] #print x ''' ''' MARK TOP MARK BOTTOM Weekly code snippet as posted 2-11-16 low-left, truncated ''' ''' #MARK TOP class node_class(): __slots__ = ('n', 'a', 'b',) def __init__(self, n=None, a=None, b=None): self.n = n self.a = a self.b = b def __repr__(self): return '(n: %s (a:(%s), b:(%s)))' % ( str(self.n), str(self.a), str(self.b)) w = node_class('base', 'left', 'right') #w.a = node_class(w.a, 'low-left', 'low-right') #print w #(n: base (a:((n: left (a:(low-left), b:(low-right)))), b:(right))) w.a = [1,2,3] a = w.a print a, w a = [1,2,4] print a, w ''' ''' def node_func(n, a, b): return type('node_func', (object,), dict(n=n, a=a, b=b)) x = node_func('base', node_func('left', 'low-left', 'low-right'), 'right' ) print x.n, x.a.n, x.a.a, x.a.b, x.b #base left low-left low-right right node_tuple = namedtuple('node', ['n', 'a', 'b']) y = node_tuple('base', node_tuple('left', 'low-left', 'low-right'), 'right') print y #node(n='base', a=node(n='left', a='low-left', b='low-right'), b='right') dict_1 = dict([('n','base'), ('a', 'left'), ('b', 'right')]) dict_2 = dict([('n','left'), ('a', 'low-left'), ('b', 'low-right')]) z = type('z', (object,), dict_1) setattr(z, 'a', type('z', (object,), dict_2)) print getattr(z, 'n'), getattr(getattr(z, 'a'), 'n'), print getattr(getattr(z, 'a'), 'a'), getattr(getattr(z, 'a'), 'b'), print getattr(z, 'b') #base left low-left low-right right #MARK BOTTOM print '\n\n\n\n\n\n' ''' ''' #This doesn't work so well class SelfInt(): def __init__(self, val=0): self = int(val) def __repr__(self): return str(int(self)) ''' ''' #Sort of interesting, # superclassed int # overloaded __add__ class MyInt(int): pass def __init__(self, val=0): int.__init__(self, val) def __add__(self, *args, **kwargs): if self == 1: return self else: return int.__add__(self, *args, **kwargs) #Test case for MyInt x = MyInt(4) y = MyInt(1) z = SelfInt(1) print x, x+x print y, y+y print x, y, x+y, y+x ''' class MyList(list): def __init__(self, a_list):#*args, **kwargs): #list.__init__() #print a_list if a_list == None: a_list = ['MyList'] else: a_list = ['MyList'] + a_list #assert isinstance(a_list, list) #print a_list #self = a_list list.__init__(self) #self += ['MyList'] self += a_list #list += a_list #print list() #m = MyList(['A']) #print m #print m.list class passClass(): class_var = 1 pc_1 = passClass() pc_1.class_var = 2 print pc_1.class_var pc_2 = passClass() print pc_2.class_var passClass.class_var = 3 print pc_1.class_var print pc_2.class_var pc_3 = passClass() print pc_3.class_var print pc_1.class_var