3

I would like to know how to increase/increment an attribute of a class.

I have a class called Account which has an attribute: self.code which is initialized at 0 and I want each time that an object is created the attribute increases.

so, for example:

account1=Account()
account2=Account()
account3=Account()

if I do account3.code I want to have 3 as a result. Is there a possible way to do so? thank you in advance.

  • 1
    You don't really want a `self.code` attribute. You want a `Account.code` class variable. – Woodford Jan 04 '22 at 16:50
  • 1
    Does this answer your question? [How to count the number of instance of a custom class?](https://stackoverflow.com/questions/25590649/how-to-count-the-number-of-instance-of-a-custom-class) – DarrylG Jan 04 '22 at 16:52
  • Does the instance variable `code` increment with each instance created in sequence? So it functions as an object-ID like a "sequence" in a database? Something like [Python object creation sequence](https://eli.thegreenplace.net/2012/04/16/python-object-creation-sequence). – hc_dev Jan 04 '22 at 17:06

5 Answers5

2

You can define a static variable for the Account object and increment it everytime you create a new object like this:

class Account:
    code = 0
    id = 0
    def __init__(self):
        self.id = Account.code
        Account.code += 1

a = Account()
print(a.id) # 0
b = Account()
print(b.id) # 1
c = Account()
print(c.id) # 2

The code variable defines how many instances were created. Every account has an id

Paul Kocian
  • 487
  • 4
  • 20
  • With the `__del__` behavior the code defines how many instances are currently alive, not how many were created. – Woodford Jan 04 '22 at 16:52
  • 2
    If `__del__` decrements the current counter the `code` will not be unique anymore. So deleting instance references (e.g. the middle of 3 with code 2) you can create another account with code 3 and then will have two accounts with same code 3 but none with 2. – hc_dev Jan 04 '22 at 18:10
  • 1
    Yes you have right, I changed the answer – Paul Kocian Jan 04 '22 at 18:11
0

you can do it like this:

class Example:
    code = 0

    def __new__(cls, *args, **kwargs):
        cls.code += 1
        return super().__new__(Example, *args, **kwargs)


ex1 = Example()
print(ex1.code) # 1
ex2 = Example()
print(ex2.code) # 2
ex3 = Example()
print(ex3.code) # 3

using __new__ is more general than __init__ in the other answers since you can create an object without calling __init__.

Robin Dillen
  • 704
  • 5
  • 11
0

If you also want each Account instance to store the code at which it was created you can use it like this

class Account:
    last_code = 0
    def __init__(self) -> None:
        self.code = Account.last_code + 1
        Account.last_code = self.code

a = Account()
b = Account()
c = Account()

print(a.code) # 1
print(b.code) # 2
print(c.code) # 3
vinzenz
  • 669
  • 3
  • 14
0

Try to use a class attribute. Example:

class Account:

    number_of_accounts = 0  # class attribute, not initialised for every instance

    def __init__(self):
        # whatever init code
        Account.number_of_accounts += 1

account_1 = Account()
account_2 = Account()
account_3 = Account()

print(Account.number_of_accounts)
0

What about a sequence stored as static class variable. Then with creation of each instance, the next value is drawn from that sequence.

import itertools

class Account:

    id_iter = itertools.count()  # sequence counter (usually used for IDs)

    def __init__(self):
        self.code = next(Account.id_iter)

See also:

hc_dev
  • 8,389
  • 1
  • 26
  • 38