Hey guys,

I've got a class defined has the following:

class TData(object) :

    #Overriden constructor
    def __init__(self, oc, via ):
	self.oc 	 = oc
        self.via	 = via

now in a separate file I'm trying it iterate through that using recursion.

def search(critia, searchSpace, value) :
	print "Calling search with " + critia
	if(len(searchSpace) > 0) :
		k=0
		i = len(searchSpace)
		k = searchSpace[0]+critia
		print k

search(".ac" , p, 1)

whilst this is not recursion in itself, i'm struggling to understand how to gain access to the class variables. The "a.c" should be appended onto searchSpace. SearchSpace is a list containing objects of TData.

When i do
print searchSpace[0].ac it works, and I would expect it to. But what I'm trying to acheive at the end of the day is that

search should take a list of parameters - these will be values such as ["ac", "via"] and the recursion will go through each of these values, so far I cant get it to dynamically evaluate the expression
searchSpace[0]+"STRING"

I Hope this makes sense.

Recommended Answers

All 6 Replies

If searchSpace[0] is an instance of class TData, then what would searchSpace[0].ac be?

searchSpace[0].ac would be an attribute belonging to that class instance.

You are trying to concatinate class instance with string ".ac" that will not work. Why don't you simply create the new class attribute dynamically this way and then you can use it.

searchSpace[0].ac = None

Maybe I'm off the mark here, see if this helps:

>>> test = TData('pork','beans')
>>> v = dir(test) # --> ['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__', 'oc', 'via']
>>> eval( "test.%s" % v[-1]) # --> 'beans'

For a class instance X, dir(X) returns a list of methods and attributes. You'll need to ignore the ones that start with "__".

searchSpace[0].ac would be an attribute belonging to that class instance.

I assumed that that was what you wanted. To handle new attributes for the class instance the way you indicated, you have to use the setattr() function. Here is an example you can explore ...

# dynamically add new attributes to a class

class C(object):
    def __init__(self, a, b):
        self.aa = a
        self.ab = b

a = C('aa', 'ab')

print a.aa  # --> 'aa'

#equivalent to a.ac = None
setattr(a, 'ac', None)
print a.ac  # --> None

# using setattr() allows to dynamically add new attributes
# from a list of attribute name strings
for attr in ['ad', 'ae', 'af']:
    # make a recognizable string
    content = attr*2
    # creates new attribute eg. a.ad = 'adad'
    setattr(a, attr, content)
    # test it ...
    # prints the contents of eg. a.ad
    print getattr(a, attr)

print '-'*13

print a.ad    # --> 'adad'
print a.af    # --> 'afaf'

# get a dictionary of attributes of class instance a
# (can be used to search contents)
attr_dic = vars(a)
print attr_dic

"""
result -->
{'aa': 'aa', 'ac': None, 'ab': 'ab', 'ae': 'aeae', 
'ad': 'adad', 'af': 'afaf'}
"""

Thanks guys, I'll play around and see what I can make of it. Any problems, i'll post back :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.