Results 1 to 5 of 5

Thread: Python help

  1. #1
    Join Date
    Dec 2005
    Location
    Cluj-Napoca, Romania
    Beans
    1,080

    Python help

    Hi!

    I need some help with 2 issues about python. I am not a complete newb but, although it seems to me that these are not difficult questions, I can not find the answer.

    1. I am trying to make a list and leave it empty but force the data type it should contain, but I don't know how.
    2. I am trying to place some data in a easily accessible manner, something like: element.propertyGroup1.property1 = value, but do it programmatically (i.e. I don't know the exact names of propertyGroup and property when I write the program). This is similar to how pyparsing can arrange data when using Dict.


    Any help will be greatly appreciated. Thank you!

  2. #2
    Join Date
    Nov 2006
    Location
    Israel
    Beans
    765
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Python help

    Both of these are somewhat unpythonic in nature. The first goes against the idea of dynamic casting. The second is a perfect dictionary application.
    Still:

    For the first you could make a class that extends the list class and override its .append method to check for a certain type, probably given to the constructor. You may also need to override some other methods in order to make sure the user doesn't change the objects type in another way.

    For the second you might be able to catch the error that comes with trying to access a non-existent member variable and use it to create the variable and initialize it to the input value - no warranty on that (might not be practical/possible).
    Last edited by smartbei; November 8th, 2007 at 09:48 AM.
    Intel E6300 / MSI P4M890M / 2GB DDR2 677 / 80GB + 1TB SATA2 / GeForce 6200TC / DL DVD+-RW / Dell 24" U2410

  3. #3
    Join Date
    Apr 2007
    Location
    Cape Town (SA)
    Beans
    Hidden!

    Re: Python help

    Not sure about the first - but I've found whenever I want to do something like that its because there's a problem in my design. I'm basically a c++ programmer so I tend to think in types. In python, you shouldn't.

    The second - have a look at the __getattr__, __setattr__ and __delattr__ special class methods. I haven't used them but it looks like what you want.

  4. #4
    Join Date
    Dec 2005
    Location
    Cluj-Napoca, Romania
    Beans
    1,080

    Re: Python help

    Thank you very much for the replies! I understand better now. I think I will drop both ideas.

    For the first issue, I will just be careful at the items I place in the list (I am the only programmer of the application). The thing was that I have a class I made and I wanted to make another class which contains an attribute that is a list of instances of the first class (complicated?). But I'll just be careful to place only that kind of objects in the list...

    For the second, I'll just use a dictionary. I got the idea from pyparsing and I htink it offers a more simple syntax, but a dict will do.

    Again, thank you very much for your answers! Take care!

  5. #5
    Join Date
    Jun 2006
    Location
    CT, USA
    Beans
    5,267
    Distro
    Ubuntu 6.10 Edgy

    Re: Python help

    Quote Originally Posted by louis_nichols View Post

    1) I am trying to make a list and leave it empty but force the data type it should contain, but I don't know how.
    When you use http://en.wikipedia.org/wiki/Duck_typing , you don't care about the exact type. If you want to code defensively, you either check if type has methods you need, or catch the exception.

    > 2) I am trying to place some data in a easily accessible manner, something like: element.propertyGroup1.property1 = value,

    If you use dictionary, key can be a tuple: element[group, property] = value.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •