Static Data?

S

Stevie_mac

I need to have a static class data member like c (see semi pseudo-like code below). How can I achieve this?

class object
static _ID
_ID = _ID + 1
self.ID = _ID

class group
. . .
def AddObject(self,obj)
objs.append(obj)
def GetObject(self,iWhich)
return objs[iWhich]

g = group()
o1=object() #gets ID of 1
o2=object() #gets ID of 2
o3=object() #gets ID of 3
g.append(o2) #store object with ID of 2
g.append(o3) #store object with ID of 3
g.append(o1) #store object with ID of 1

print g.GetObject(0).ID #print ID of group object 0
print g.GetObject(1).ID #print ID of group object 1
print g.GetObject(2).ID #print ID of group object 2

obviously this code is duff, its the solution I need.

Cheers Stevie_Mac.
 
P

Peter Otten

Stevie_mac said:
I need to have a static class data member like c (see semi pseudo-like
code below). How can I achieve this?

class Object:
# Python already has a builtin object class;
# use uppercase Object to avoid nameclash
_ID = 1
def __init__(self):
self.ID = Object._ID
Object._ID += 1

# An alternative implementation
# import itertools
# class Object:
# IDGen = itertools.count(1)
# def __init__(self):
# self.ID = self.IDGen.next() # successive calls yield 1, 2, 3, ...

class Group(dict):
# you could make this more dict-like
def addObject(self, obj):
self[obj.ID] = obj
def getObject(self, iWhich):
return self[iWhich]

g = Group()

o1 = Object()
o2 = Object()
o3 = Object()

g.addObject(o2)
g.addObject(o3)
g.addObject(o1)

print g.getObject(1).ID
print g.getObject(2).ID
print g.getObject(3).ID

The above works, but I've got a hunch that you are on the wrong track here.
Most likely you should pass o1, o2, ... around instead of the IDs.

Peter
 
S

Stevie_mac

Stevie_mac said:
I need to have a static class data member like c. How can I achieve this?
<SNIP>


Thanks all for your assistance. This though raised some question for me...

In Jeffs solution, Is self._ID the same variable as _ID (declared immediately after class Counted). Is there 3 _IDs?
If you have the time, could you explain (nothing fancy, just brief) the meaning of declaring variables in different
locations (like self, below class, as part of class, in a class def). This would be greatly appreciated.

and Peters solution
class Object:
_ID = 1
def __init__(self):
self.ID = Object._ID
Object._ID += 1
Is Object._ID a class instance variable and self.ID a 'shared' class variable

Cheers - Stevie_Mac (confused)
 
J

John Roth

Stevie_mac said:
I need to have a static class data member like c (see semi pseudo-like
code below). How can I achieve this?

You want an updatable class member. There are two ways
of doing this.

1) Reference the class directly:

class MyObject:
_id = 0

def __init__(self):
MyObject._id += 1
self.ID = _id

2) use a class method:

class MyObject(object):
_id = 0

def nextID(klas):
klas._id += 1
return klas._id
nextID = classmethod(nextID)

def __init__(self):
self.ID = self.nextID()

Method 1 works in all releases of Python. Method 2
requires release 2.2 or later.

HTH

John Roth
 
P

Peter Otten

Stevie_mac said:
In Jeffs solution, Is self._ID the same variable as _ID (declared
immediately after class Counted). Is there 3 _IDs? If you have the time,

Jeff Epler's code with annotations:

class Counted:
_ID = 0 # set the class attribute; same as Counted._ID = 0

def __init__(self):
Counted._ID += 1 # increment the class attribute
self._ID = Counted._ID # set the instance attribute to the
# current value of the class attribute
could you explain (nothing fancy, just brief) the meaning of declaring
variables in different
locations (like self, below class, as part of class, in a class def).
This would be greatly appreciated.

and Peters solution
class Object:
_ID = 1
def __init__(self):
self.ID = Object._ID
Object._ID += 1
Is Object._ID a class instance variable and self.ID a 'shared' class
variable

This is basically the same as Jeff's. I just chose different names for the
class attribute (_ID) and instance attribute (ID).

If you set an attribute in the class,

class Object:
clsattr = 1

that is the same as

class Object:
pass
Object.clsattr = 1

If you set an instance attribute you always need the instance:

inst = Object()
inst.attr = 2

Inside a method it's the same, only the instance will most likely be named
self.

class Object:
def method(self, newvalue):
self.attr = newvalue

Now for attribute lookup:

inst.attr

will first look up attr in the instance, and if it's not there, it will fall
back to the class:
.... pass
....'in instance'

The class attribute is still there, only "shaded" bye the instance
attribute:
'in class'

Now let's delete the instance attribute:
'in class'

The class attribute becomes visible again.

Once you have understood the above, classmethods are just a another way to
access the class instead of the instance. The advantage over an explicit
use of the class identifier is that with inheritance they always provide
the actual class:
.... def method(cls):
.... print cls.__name__
.... method = classmethod(method)
........ pass
....Sub

Peter
 
S

Stevie_mac

Thanks Peter, I really appreciate this - I'm gonna trim this down to a short tutorial - I personally think (even having
read up on classes & scope) that this was the toughest thing to get my head around.
 

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

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,978
Members
47,561
Latest member
gjsign

Latest Threads

Top