Home > Software engineering >  How to count the Index of an instance?
How to count the Index of an instance?

Time:11-17

I want to start multiple servers like this:

Server 1
Server 2
Server 3

I have already written a code:

class Server:
     def __init__(self, index):
          self.index = index

My main is looking like this:

server1 = Server(1)
server1.run()

So my question is as you can read in the captio: How can I global count the Index of different instances (Servers) dynamically?

CodePudding user response:

Store the total instance count in a class variable.

class Server:
    instances = 0   
    def __init__(self):
         Server.instances  = 1
         self.index = Server.instances
  • Related