Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Class subscripting

Reply
Thread Tools

Class subscripting

 
 
Ronny Mandal
Guest
Posts: n/a
 
      02-18-2006
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
 
Reply With Quote
 
 
 
 
Ian Leitch
Guest
Posts: n/a
 
      02-18-2006
-----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)

iD8DBQFD9msAefZ4eWAXRGIRAjN0AJ40pfI5fhm2WK7gV1Q4dE r9Nv10TgCgo6mY
ovWBqjXvdSjH3zNh61j5qj4=
=XrYe
-----END PGP SIGNATURE-----
 
Reply With Quote
 
 
 
 
Larry Bates
Guest
Posts: n/a
 
      02-18-2006
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
 
Reply With Quote
 
skip@pobox.com
Guest
Posts: n/a
 
      02-18-2006

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/...nce-types.html

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

Skip

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      02-18-2006
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
About matrix subscripting index d major C++ 7 03-30-2008 09:58 AM
std::string, subscripting, and npos Marcus Kwok C++ 7 01-27-2006 09:51 PM
Array subscripting overflow behaviour Netocrat C Programming 7 06-29-2005 12:34 PM
array subscripting buda C Programming 10 11-15-2004 07:54 PM
vector performance: iterator versus subscripting Matt Garman C++ 4 11-26-2003 01:58 PM



Advertisments