Employee Class using Instance & Class Variables

Challenge: Employee Class using Instance & Class Variables

Objective: Create an Employee class that uses both instance variables and a class variable.


1. Class Properties (Variables)

  • Instance Variables: (Unique to each employee object)
    • name
    • salary
    • designation
    • employee_id (Automatically assigned, not taken as a parameter)
  • Class Variable: (Shared across all employee objects)
    • employee_count
    • Initial Value: 101
    • Purpose: Used to automatically generate the next employee ID.

2. Employee ID Rules

  • The first employee ID should be E101.
  • The next should be E102, and so on.
  • The ID must be assigned automatically when a new employee object is created.
  • The employee_count class variable must be incremented after assigning an ID.

3. Class Methods (Functions)

  • __init__(self, name, salary, designation)
    • Takes namesalary, and designation as parameters.
    • Does NOT take employee_id as a parameter.
    • Inside this function:
      • Assigns an employee_id using the current employee_count (e.g., “E101”).
      • Increments the employee_count class variable for the next employee.
  • show_details(self)
    • Prints all the details of the employee: Name, Salary, Employee ID, and Designation.
  • total_employees(cls) (A Class Method)
    • Correction: This must be a class method, not a static method.
    • Purpose: Returns the total number of employees created.
    • Logic: Calculate total employees as employee_count - 101.

4. Key Summary

  • Use a class variable (employee_count) to auto-generate IDs.
  • Use instance variables to store individual employee details.
  • Implement three methods__init__show_details, and the class method total_employees.

class Employee:

    employee_count = 101

    def __init__(self, name, desig, sal):
        self.name = name
        self.designation = desig
        self.salary = sal
        self.eid = 'e' + str(Employee.employee_count)
        Employee.employee_count += 1

    def show_details(self):
        print('Name:',self.name)
        print('Eid:',self.eid)
        print('Designation:',self.designation)
        print('Salary:',self.salary)

    @classmethod
    def total_emp(cls):
        return cls.employee_count - 101


e1 = Employee('John', 'Manager', 10000)
e2 = Employee('Mark', 'Team Leader', 8000)

e1.show_details()
print('')
e2.show_details()
print('Total Employees:', e1.total_emp())

Similar Posts

  • Programs

    Weekly Wages Removing Duplicates even ,odd Palindrome  Rotate list Shuffle a List Python random Module Explained with Examples The random module in Python provides functions for generating pseudo-random numbers and performing random operations. Here’s a detailed explanation with three examples for each important method: Basic Random Number Generation 1. random.random() Returns a random float between 0.0 and 1.0 python import…

  • re module

    The re module is Python’s built-in module for regular expressions (regex). It provides functions and methods to work with strings using pattern matching, allowing you to search, extract, replace, and split text based on complex patterns. Key Functions in the re Module 1. Searching and Matching python import re text = “The quick brown fox jumps over the lazy dog” # re.search()…

  • What is PyCharm? Uses, History, and Step-by-Step Installation Guide

    What is PyCharm? PyCharm is a popular Integrated Development Environment (IDE) specifically designed for Python development. It is developed by JetBrains and is widely used by Python developers for its powerful features, ease of use, and support for various Python frameworks and tools. PyCharm is available in two editions: Uses of PyCharm PyCharm is a…

  • Negative lookbehind assertion

    A negative lookbehind assertion in Python’s re module is a zero-width assertion that checks if a pattern is not present immediately before the current position. It is written as (?<!…). It’s the opposite of a positive lookbehind and allows you to exclude matches based on what precedes them. Similar to the positive lookbehind, the pattern…

  • AttributeError: ‘NoneType’ Error in Python re

    AttributeError: ‘NoneType’ Error in Python re This error occurs when you try to call match object methods on None instead of an actual match object. It’s one of the most common errors when working with Python’s regex module. Why This Happens: The re.search(), re.match(), and re.fullmatch() functions return: When you try to call methods like .group(), .start(), or .span() on None, you get this error. Example That Causes…

Leave a Reply

Your email address will not be published. Required fields are marked *