-1

I want to give each employee I create in the Employee class an ID but automatically

Here is the code:

class employees :
    def  __init__(self,first,last,pay):
        self.first_name = first
        self.last_name = last
        self.pay = pay
        self.email = first + '.' + last + '@company.com'
        self.full_name = first + ' '+last
        self.facebook_link = 'FB.com/'+ self.full_name
foxyblue
  • 2,859
  • 2
  • 21
  • 29
Mohammed Breky
  • 385
  • 3
  • 9
  • For future reference, snippets only work for HTML, CSS, or javascript – user3483203 Jun 27 '18 at 21:18
  • By "countable" what do you mean? The easiest way to do this would be to create a uuid for each – Matthew Story Jun 27 '18 at 21:19
  • example : emp_1 have id 1 emp_2 have id 2 emp_33 have id 33 and so on – Mohammed Breky Jun 27 '18 at 21:20
  • across different runs of the app on different servers? – Matthew Story Jun 27 '18 at 21:21
  • @MatthewStory i want to give an ID for each employee like in SQL is identity . – Mohammed Breky Jun 27 '18 at 21:23
  • class employees : counter = 0 def __init__(self,first,last,pay): self.first_name = first self.last_name = last self.pay = pay self.email = first + '.' + last + '@company.com' self.full_name = first + ' '+last self.facebook_link = 'FB.com/'+ self.full_name self.employee_id = employees.counter + 1 employees.counter += 1 – Joshua Schlichting Jun 27 '18 at 21:27
  • Wow, that was ugly - Just place a static counter variable in the class, assign it as the ID, and increment it immediately after assigning the ID within the __init__() function. – Joshua Schlichting Jun 27 '18 at 21:28
  • SLOVED THANKS ALOT i find it in : https://stackoverflow.com/questions/1045344/how-do-you-create-an-incremental-id-in-a-python-class?noredirect=1&lq=1 – Mohammed Breky Jun 27 '18 at 21:33

1 Answers1

1
class employees:
    uid = 0
    def  __init__(self,first,last,pay):
        self.first_name = first
        self.last_name = last
        self.pay = pay
        self.email = first + '.' + last + '@company.com'
        self.full_name = first + ' '+last
        self.facebook_link = 'FB.com/'+ self.full_name

        employees.uid += 1
        self.uid = employees.uid

Now when I created by instances and print their uid

emp1 = employees('Abhishek', 'Babuji', 1000)
print(emp1.uid)

Output: 1

emp2 = employees('Abhishek1', 'Babuji1', 10001)
print(emp2.uid)

Output: 2

Every time you go inside __init__ employees.uid gets incremented by 1 and then it is assigned to the instance usingself.uid

ababuji
  • 1,683
  • 2
  • 14
  • 39