[Tutor] extracting lists from lists of lists

Kent Johnson kent_johnson at skillsoft.com
Tue Sep 14 17:36:26 CEST 2004


OK, thanks for the explanation!

I'm still thinking a class is a good idea. You want a data abstraction that 
is easy to use and whose use is clear. Classes are good at this. For 
example you could create an asTuple() method to return the class data as a 
tuple for use in the database calls. You can use class properties and 
__slots__ to make it so fields can't be added to or deleted from the class. 
You have a place to put other methods that work with the same data.

If you use a dict to hold the data, users can delete entries in their own 
code, they don't even have to change the source you give them. Plus the 
code to convert to a tuple has no home unless you make a class to wrap the 
dict, but then you're back to using a class.

For the database access, you might want to provide some kind of wrapper 
class that makes it easy for users to access the database correctly. The 
example you give is fragile with any implementation of the data - if a 
table column is added or the order of columns in the insert is changed then 
the insert will break.

If you are giving the users complete source, you can't really make the 
application bulletproof. Your best bet is to provide useful abstractions 
organized into functional layers so they aren't tempted to change your code 
so much.

By the way your class definition should assign the instance variables in 
the constructor, something like this:
class myData:
   def __init__(self, name, age, gender):
     self.name = name
     self.age = age
     self.gender = gender

   def asTuple(self):
     return (self.name, self.age, self.gender)

And what is wrong with C++ member variables? A C++ struct is just a class 
with no methods, just fields.

Kent

At 04:58 PM 9/14/2004 +0200, nik wrote:
>Kent Johnson wrote:
>
>>I still don't understand why you can't use a simple class for this. You 
>>can pass around instances of the class instead of tuples. You can make 
>>lists or sets of class instances when you need more than one.
>>
>>Or if you decide to use a dict, you can pass that directly to clients 
>>instead of making it into a tuple.
>>
>>Can you give an example of how you will use the tuples?
>>
>>Finally, you might be interested in some recipes in the online Python 
>>Cookbook (http://aspn.activestate.com/ASPN/Cookbook/Python) for making 
>>tuples with named members. They are immutable though, I think you said 
>>you want users to be able to change the values.
>>
>>Kent
>
>The tuples are for the kinterbasDB commands, like;
>
>newPeople = (
>    ('Lebed'       , 53),
>    ('Zhirinovsky' , 57),
>  )
>
>for person in newPeople:
>    cur.execute("insert into people (name_last, age) values (?, ?)", person)
>
>That's the only place I need a tuple from my data structure, I have no 
>other need to pass a tuple anywhere. My (C++) application continually 
>creates the data structures which ends up in a list in a python module. 
>The items in the list can then pass through various actions like values 
>getting changed (mapping or mathmatical operations), database entry and 
>manipulation, or even sent through a socket to somewhere else.
>All the actions are in various python scripts which I'm allowing my users 
>to edit freely (since they all have different requirements). I'd like to 
>keep their life as simple as possible, and at the heart of that is this 
>data structure.
>
>Maybe I'm thinking too hard about it, and the original
>
>class myData:
>   name = ""
>   age = ""
>   gender = ""
>
>was actually the simplest and clearest. I was concerned that a user would 
>delete some of the items, causing problems later in the system. Also 
>possibly I'm hung up on the idea of member variables in C++?
>
>nik
>
>>At 03:45 PM 9/14/2004 +0200, nik wrote:
>>
>>>Kent Johnson wrote:
>>>
>>>>At 12:52 PM 9/14/2004 +0200, nik wrote:
>>>>
>>>>>hi,
>>>>>
>>>>>I have a class which is just a holder for a data structure, and for 
>>>>>some reason I've decided to hold the data in the following form;
>>>>>
>>>>>class myData:
>>>>>    data = [  ["name", ""], ["age", ""], ["gender", ""] ]
>>>>>
>>>>>I'm not totally sure it's the best way, but it strikes me as something 
>>>>>that can be easily manipulated into maps etc. I had started out with
>>>>>class myData:
>>>>>    name = ""
>>>>>    age = ""
>>>>>    gender = ""
>>>>>
>>>>>but I found that I had to put most of those items into lists to do 
>>>>>anything with them.
>>>>
>>>>
>>>>
>>>>What kinds of things are you doing that you need these values in a 
>>>>list? It sounds like maybe you are trying to pull the data out of the 
>>>>class to pass to another function, but maybe it would be better to pass 
>>>>the class itself around? Or maybe you should get rid of the class 
>>>>entirely and just use a dictionary.
>>>
>>>
>>>I'm thinking in terms of a C struct -  it's a handy parcel to move a set 
>>>of data around, and quite likely there'll be stacks of sets. There's no 
>>>extra functionality required from the set, and I'm not planning on 
>>>deriving any other classes from this one. The dictionary seems a very 
>>>good idea, but I think I still need to put it into a class since I'll 
>>>have multiple instances of it - is that right? or can I do the 
>>>equivalent of a typedef?
>>
>>
>>_______________________________________________
>>Tutor maillist  -  Tutor at python.org
>>http://mail.python.org/mailman/listinfo/tutor
>>
>
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list