I need to extract data from external source. I build a base clas with some structure. I need objects with type of myBase class but with a name which automatically describes how many times I iterate over the source. I build a class which stores this number and with a call method which returns the object I need. I can’t find a way to have a string as the name for the object but automatically differ every time I build new object.
I simplified the code for this example:
class myBase:
def __init__(self):
iteratedValue = None
class myIterator:
def __init__(self):
self.k = 0
def __call__(self, s):
self.k += 1
self.name = 'myData' + str(self.k)
# create an object named myData1, myData2 ...
self.name = myBase()
self.name.iteratedValue = s
print '%s name is %s for k = %i' % (self.name, self.name.iteratedValue, self.k)
# return object named myData1 type of myBase
return self.name
def DataExtraction():
# function to extract data form external source
data = myIterator()
data('Alice')
data('Ben')
DataExtraction()
My output is:
<__main__.myBase instance at 0x7f261b6dc6c8> name is Alice for k = 1
<__main__.myBase instance at 0x7f261b6dc6c8> name is Ben for k = 2
I need to return an object with a specific name and I expect an output:
myData1 name is Alice for k = 1
myData2 name is Ben for k = 2
The original problem is much more complicated. I have external data and every time they come I want to extract some values from this data. Every time I am doing this I need an object to work with it but I need this object with different names because on the end I store them in dict for another methods. In other words I work with data and store my results in the object myData1 when the original data change I work with it again and store the results in myData2 etc. After all I need all myData objects and do statistics on them to see how the change. I do not have access to original data any more. I need automatic name convention for myData and the best if it will express iterator.
How can I have a string in place of self.name as the name for an object?
You’re already doing it with
self.name = 'myData' + str(self.k). The problem is that you immediately overwrite it withself.name = myBase(). I’m not sure what you’re trying to do with thatmyBase(), but you probably want to separate it from the name.self.namecan be either a string or amyBaseobject, but it can’t be both.Perhaps you can do:
This way you can have both the name and the “base”, as separate attributes
self.nameandself.base.Alternatively, you can give the
myBaseclass a__str__method. This will affect what shows up when you useprinton amyBaseobject. However, to do that you’ll have to pass in the desired name when you instantiatemyBase, something like:I’m not sure which of these ways (or perhaps some other way) is what you’re looking for. What’s puzzling is that you are making the “name” of the object be a
myBaseinstance. I’m not sure what you’re intending the “name” of your object to represent, but I wouldn’t usually expect an object’s name to be some other object.Incidentally, it looks like you’re using Python 2, in which case you should define your class with
class someClass(object). Including theobjectmakes your classes new-style classes, which is basically what you always want.Edit: If what you’re trying to do is actually create a variable called
myData1based on the string, so you can domyData('Alice')and then somehow magically have the variablemyData1refer to that object, the answer is “Don’t do that.” If you want to create a bunch of objects and access them in a structured way by numbers or other labels, use a list or a dictionary.