Help with Dictionaries and Classes requested please.

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Thu Aug 9 11:42:20 EDT 2007


Neil Cerutti a écrit :
> On 2007-08-09, Bruno Desthuilliers <bruno.42.desthuilliers at wtf.websiteburo.oops.com> wrote:
>> Neil Cerutti a écrit :
>>> On 2007-08-09, special_dragonfly <Dominic at PLEASEASK.co.uk> wrote:
>>>> Is there anyway for python to consider the values within a
>>>> string when entering the data into a dictionary. I know that
>>>> isn't very clear so here's an example:
>>>>
>>>> class MyClass(object):
>>>>     def __init__(self,name="",age=""):
>>>>         self.name=name
>>>>         self.age=age
>>>>
>>>> data="Gary,50"
>>>> d={0:[MyClass(data)]}
>>>> data="Adam,25"
>>>> d[0].append(MyClass(data))
>>>>
>>>> The data is coming from a text file working on a line by line
>>>> basis. I've just tried and I'm just getting the full string in
>>>> the first field. That seems logical, now I don't want it to
>>>> though!
>>> That's what happens if you use 0 for the key every time. ;)
>> Hmmm... Neil, I may be wrong but I think you didn't get the
>> point here. As I understand it,  Dominic's problem is that it
>> gets strings like "Gary,50" and would like to call MyClass
>> initializer this way : MyClass("Gary", "50")
> 
> My guess was he doesn't need a class at all,

Mmm... That's possible (and if all he has in MyClass are name and age 
data attributes, then you're obviously right). But then your answer was 
perhaps a bit confusing (at least it confused me...)




More information about the Python-list mailing list