def hasName(obj, name): #Not in the C version """Determine if 'obj' has the 'name' """ key = name.split('.')[0] if not hasKey(obj, key): return False try: valueForName(obj, name) return True except NotFound: return False
def example(): class A(Mixin): classVar = 'classVar val' def method(self, arg='method 1 default arg'): return arg def method2(self, arg='meth 2 default arg'): return {'item1': arg} def method3(self, arg='meth 3 default'): return arg class B(A): classBvar = 'classBvar val' a = A() a.one = 'valueForOne' def function(whichOne='default'): values = { 'default': 'default output', 'one': 'output option one', 'two': 'output option two' } return values[whichOne] a.dic = { 'func': function, 'method': a.method3, 'item': 'itemval', 'subDict': { 'nestedMethod': a.method3 } } b = 'this is local b' print(valueForKey(a.dic, 'subDict')) print(valueForName(a, 'dic.item')) print(valueForName(vars(), 'b')) print(valueForName(__builtins__, 'dir')()) print(valueForName(vars(), 'a.classVar')) print(valueForName(vars(), 'a.dic.func', executeCallables=True)) print(valueForName(vars(), 'a.method2.item1', executeCallables=True))
def example(): class A(Mixin): classVar = 'classVar val' def method(self,arg='method 1 default arg'): return arg def method2(self, arg='meth 2 default arg'): return {'item1':arg} def method3(self, arg='meth 3 default'): return arg class B(A): classBvar = 'classBvar val' a = A() a.one = 'valueForOne' def function(whichOne='default'): values = { 'default': 'default output', 'one': 'output option one', 'two': 'output option two' } return values[whichOne] a.dic = { 'func': function, 'method': a.method3, 'item': 'itemval', 'subDict': {'nestedMethod':a.method3} } b = 'this is local b' print(valueForKey(a.dic, 'subDict')) print(valueForName(a, 'dic.item')) print(valueForName(vars(), 'b')) print(valueForName(__builtins__, 'dir')()) print(valueForName(vars(), 'a.classVar')) print(valueForName(vars(), 'a.dic.func', executeCallables=True)) print(valueForName(vars(), 'a.method2.item1', executeCallables=True))
def valueForName(self, name): return valueForName(self, name)