Class subscripting

Discussion in 'Python' started by Ronny Mandal, Feb 18, 2006.

  1. Ronny Mandal

    Ronny Mandal Guest

    Assume we have a class Foo, and instance called bar.

    a variable called baz1 has the value 3.0, baz2 is uninitialized

    Is there a way of reflecting the variable with such syntax:

    print bar[<var_index>], where var_index is a number representing
    internal index.

    bar[<var_index>] = 4.2. #Setting baz2 to 4.2

    Thanks and regards,

    Ronny Mandal
     
    Ronny Mandal, Feb 18, 2006
    #1
    1. Advertising

  2. Ronny Mandal

    Ian Leitch Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Ronny Mandal wrote:
    > Assume we have a class Foo, and instance called bar.
    >
    > a variable called baz1 has the value 3.0, baz2 is uninitialized
    >
    > Is there a way of reflecting the variable with such syntax:
    >
    > print bar[<var_index>], where var_index is a number representing
    > internal index.
    >
    > bar[<var_index>] = 4.2. #Setting baz2 to 4.2


    Hmmm... I don't like this much, though it'd could be a little better if
    you kept a separate list which you update in __setitem__ instead of
    constructing a new list for each __getitem__ call.

    I'm sure someone has a more elegant solution, I'm afraid I'm too tired
    to come up with anything better.

    Oh, and don't forget to check that key is an IntType!

    class MyClass:

    def __init__(self):

    self.a = "I'm a!"
    self.b = "I'm b!"

    def __getitem__(self, key):

    if key != None:

    return list(self.__dict__.values())[key]

    x = MyClass()
    print x[0]
    print x[1]


    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.1 (GNU/Linux)

    iD8DBQFD9msAefZ4eWAXRGIRAjN0AJ40pfI5fhm2WK7gV1Q4dEr9Nv10TgCgo6mY
    ovWBqjXvdSjH3zNh61j5qj4=
    =XrYe
    -----END PGP SIGNATURE-----
     
    Ian Leitch, Feb 18, 2006
    #2
    1. Advertising

  3. Ronny Mandal

    Larry Bates Guest

    Ronny Mandal wrote:
    > Assume we have a class Foo, and instance called bar.
    >
    > a variable called baz1 has the value 3.0, baz2 is uninitialized
    >
    > Is there a way of reflecting the variable with such syntax:
    >
    > print bar[<var_index>], where var_index is a number representing
    > internal index.
    >
    > bar[<var_index>] = 4.2. #Setting baz2 to 4.2
    >
    > Thanks and regards,
    >
    > Ronny Mandal


    I can't figure out why did you tell us that baz1 has value of
    3.0? What does that have to do with anything? It is also
    uncloear how you think that setting something inside bar
    class instance could possibly create a variable called
    baz2? Python doesn't have a concept of "unitialized" variables.
    Before they are initialized they simply don't exist. Variables
    in Python aren't buckets, variables are pointers to information.
    I think we need more information about what you are trying to
    accomplish before we can help.

    Just as a wild guess I think you want a dictionary.

    vdict=['baz1': 3.0, 'baz2': None}

    vdict['baz2']=4.2

    print vdict['baz2']

    if you insist on an index you can do:

    vars=['baz1','baz2']

    print vdict[vars[2]]

    I'm REALLY guessing here, so I don't know if I'm helping.

    -Larry Bates
     
    Larry Bates, Feb 18, 2006
    #3
  4. Ronny Mandal

    Guest

    Ronny> Assume we have a class Foo, and instance called bar. a variable
    Ronny> called baz1 has the value 3.0, baz2 is uninitialized

    Ronny> Is there a way of reflecting the variable with such syntax:

    Ronny> print bar[<var_index>], where var_index is a number representing
    Ronny> internal index.

    Ronny> bar[<var_index>] = 4.2. #Setting baz2 to 4.2

    Sure. Check out the special method names for container types:

    http://www.python.org/dev/doc/devel/ref/sequence-types.html

    To index into instances of your class you need to define a __getitem__
    method.

    Skip
     
    , Feb 18, 2006
    #4
  5. On Sat, 18 Feb 2006 01:09:22 +0100, Ronny Mandal wrote:

    > Assume we have a class Foo, and instance called bar.
    >
    > a variable called baz1 has the value 3.0, baz2 is uninitialized


    Python doesn't have variables. It has names which are bound to objects. Do
    you mean that the name baz1 is bound to the value 3.0?

    Because Python has no variables, you can't have uninitialized variables.
    You can have names which are bound to values (objects), and you can have
    names which don't exist yet. Do you mean that baz2 is a name which doesn't
    yet exist?

    In other words, just so we are clear, at this point we have the following
    Python code:


    class Foo:
    pass

    bar = Foo()
    baz1 = 3.0
    # baz2 not yet used.



    > Is there a way of reflecting the variable with such syntax:
    >
    > print bar[<var_index>], where var_index is a number representing
    > internal index.
    >
    > bar[<var_index>] = 4.2. #Setting baz2 to 4.2



    No. But you can do better:

    baz = {} # holder for the values of the bazmatron.
    baz[1] = 3.0
    baz[2] = 4.2
    baz[25] = 3.9

    # Check if we have the third value for the bazmatron.

    if baz.has_key(3): # "Look before you leap"
    print baz[3]

    # Another way to do the same thing.
    try:
    print baz[3]
    except KeyError:
    print "No third value. Initializing it now."
    baz[3] = 0.0

    # A third way.
    print baz.get(3, 0) # Prints 0 if no third value exists.

    # A fourth way: print baz[3] with a default value of 0,
    # and set the value if it doesn't already exist.
    print baz.setdefault(3, 0)



    --
    Steven.
     
    Steven D'Aprano, Feb 18, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt Garman
    Replies:
    4
    Views:
    4,183
    Ivan Vecerina
    Nov 26, 2003
  2. buda

    array subscripting

    buda, Nov 2, 2004, in forum: C Programming
    Replies:
    10
    Views:
    635
    Lawrence Kirby
    Nov 15, 2004
  3. Netocrat

    Array subscripting overflow behaviour

    Netocrat, Jun 28, 2005, in forum: C Programming
    Replies:
    7
    Views:
    358
    Lawrence Kirby
    Jun 29, 2005
  4. Marcus Kwok
    Replies:
    7
    Views:
    449
    Duane Hebert
    Jan 27, 2006
  5. d major
    Replies:
    7
    Views:
    292
    James Kanze
    Mar 30, 2008
Loading...

Share This Page