tags:

views:

328

answers:

4

I would like to create a unique ID for each object I created - here's the class:

class resource_cl :
    def __init__(self, Name, Position, Type, Active):
        self.Name = Name
        self.Position = Position
        self.Type = Type
        self.Active = Active

I would like to have a self.ID that auto increments everytime I create a new reference to the class, such as:

resources = []
resources.append(resource_cl('Sam Sneed', 'Programmer', 'full time', True))

I know I can reference resource_cl, but I'm not sure how to proceed from there...

+6  A: 

First, use Uppercase Names for Classes. lowercase names for attributes.

class Resource( object ):
    class_counter= 0
    def __init__(self, name, position, type, active):
        self.name = name
        self.position = position
        self.type = type
        self.active = active
        self.id= Resource.class_counter
        Resource.class_counter += 1
S.Lott
Inside __init__, the class_counter reference is a property of the class. It should look like this:Resource.class_counter += 1
scompt.com
S.Lott, I did try it. With your first version, every object will have an id of 0, and a class_counter of 1.
Matthew Flaschen
self will work in the case of accessing the value. So:self.id= self.class_counterWill behave as desired as the attribute resolution will fall back to the class property. This is not the case for setting the attribute as the value set will be in the scope of the instance and not the underlying class.
Mark Roddy
The += operator can have very surprising results because it works differently for different types. And not just on [im]mutability lines either, e.g. a mutable class with __add__ but not (the perhaps forgotten) __iadd__.
Roger Pate
+3  A: 

Using count from itertools is great for this:

>>> import itertools
>>> counter = itertools.count()
>>> a = next(counter)
>>> print a
0
>>> print next(counter)
1
>>> print next(counter)
2
>>> class A(object):
...   id_generator = itertools.count(100) # first generated is 100
...   def __init__(self):
...     self.id = next(self.id_generator)
>>> objs = [A(), A()]
>>> print objs[0].id, objs[1].id
100 101
>>> print next(counter) # each instance is independent
3

The same interface works if you later need to change how the values are generated, you just change the definition of id_generator.

Roger Pate
+3  A: 

Are you aware of the id function in python, and could you use it instead of your counter idea?

class C(): pass

x = C()
y = C()
print id(x), id(y)    #(4400352, 16982704)
llimllib
I used id(self) within the class - seems to be the right solution for my needs - thanks
meade
Remember that values returned by id() are not guaranteed to be unique within one execution of the program (they may be reused as objects are collected). Nor are they guaranteed to follow any specific pattern (you did ask for auto-incrementing originally).
Roger Pate
R. Pate: absolutely correct, and important to keep in mind; I just wanted to make sure he was aware of the id function's existence and evaluated it properly before choosing to do something manually.
llimllib
+1  A: 

Concise and elegant:

import itertools

class resource_cl():
    newid = itertools.count().next
    def __init__(self):
        self.id = resource_cl.newid()
        ...
Algorias