Class subscripting

R

Ronny Mandal

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

Ian Leitch

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

Ronny said:
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-----
 
L

Larry Bates

Ronny said:
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
 
S

skip

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
 
S

Steven D'Aprano

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)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top