jrosco
6/8/2015 - 6:57 AM

props_n_classes.py

class SetterNGetter():
	
    def __init__(self, *args, **kwargs):
        self.my_age = kwargs['age']
        self.my_name = kwargs['name']

    @property
    def my_age(self):
        return self.my_age

    @my_age.setter
    def my_age(self, value):
        self.my_age = value
	
    @my_age.deleter 
    def my_age(self):
	    del(self.my_age)

    @property
    def my_name(self):
	    return self.my_name

    @my_name.setter
    def my_name(self, value):
	    self.my_name = value

    @my_name.deleter
    def my_name(self):
	    del(self.my_name)

	
def check_age(my_func):
	
	def check(*args, **kwargs):
		print_young(kwargs['age'], kwargs['name']) if kwargs['age'] < 40 else print_old(kwargs['age'], kwargs['name'])
		
	def print_young(age, name):
		print '\nHi, %s\n %i is young' % (name, age)
	
	def print_old(age, name):
		
		if age >= 100:
			print '\nHi, %s\n At %i years of age you should be dead' % (name, age) 
		else:
			print '\nHi, %s\n %i is old' % (name, age)
		
	return check
	
@check_age
def do_age_check(*args, **kwargs):
	return args, kwargs

	
if __name__ == "__main__":
	go_get = SetterNGetter(age=[23], name=['Dick'])
	
	go_get.my_age.append(24)
	go_get.my_name.append('Fred')
	
	go_get.my_age.append(100)
	go_get.my_name.append('Bob')
	
	go_get.my_age.append(74)
	go_get.my_name.append('Tom')
	
	for i, a in enumerate(go_get.my_age):
		do_age_check(age=a, name=go_get.my_name[i])