Palla veera
Palla veera

Reputation: 123

Python Instance Variable assignment standard Programming Practice

I have to define Instance variable, This Instance Variable is accessed in different Instance methods. Hence I am setting up Instance Variable under constructor. I see best of Initializing instance variables under constructor.

Is it a Good practice to use if else condition under constructor to define instance variable. Is there any other pythonic way to achieve this in standard coding practice.

class Test:
    def __init__(self, EmpName, Team):
        self.EmpName = EmpName
        self.Team = Team
        if Team == "Dev":
            self.Manager = "Bob"
        elif Team == "QA":
            self.Manager = "Kim"
        elif Team == "Admin":
            self.Manager == "Jeff" 

Upvotes: 0

Views: 64

Answers (4)

vinodsesetti
vinodsesetti

Reputation: 418

class Test():
    def __init__(self,EmpName = "",Team = ""):
        self.EmpName = EmpName
        self.Team = Team
        self.Manager = Manager
        if self.Team == "Dev":
            self.Manager = "Bob"
        elif self.Team == "Dev":
            self.Manager = "Kim"
        elif self.Team == "Admin":
            self.manager = "Jeff"
        else:
            self.manager = ""
    def get_manager(self):
        return self.Manager

obj = Test("DemoEmp","Dev")
print (obj.get_manager())

Upvotes: 0

Grismar
Grismar

Reputation: 31354

It's very bad coding practice to mix data and structure like this in general for object oriented programming and Python is no exception. There's a number of ways to solve this:

  • you could just passing in the team manager; but it appears that's the step you want to automate
  • you could link the Employee to a team instance, something like:

Something like:

class Team:
    def __init__(self, name, manager):
        self.name = name
        self.manager = manager


class Employee:
    def __init__(self, name, team):
        self.name = name
        self.team = team


team_dev = Team("Dev", "Bob")
team_qa = Team("QA", "Kim")
employee_1 = Employee("Jack", team_dev)

print(f'{employee_1.name} is on {employee_1.team.name} as managed by {employee_1.team.manager}')

Note that variables should not be CamelCase, but class names should.

An even nicer solution might be to have the manager of a team be an employee themselves. Of course, then you need the team when you create the employee and the employee when you create the team, so it may need to be None initially:

class Team:
    def __init__(self, name, manager=None):
        self.name = name
        self.manager = manager


class Employee:
    def __init__(self, name, team):
        self.name = name
        self.team = team


team_dev = Team("Dev")
team_dev.manager = Employee("Bob", team_dev)
team_qa = Team("QA")
team_qa.manager = Employee("Kim", team_qa)
employee_1 = Employee("Jack", team_dev)

print(f'{employee_1.name} is on {employee_1.team.name} as managed by {employee_1.team.manager.name}')

Upvotes: 1

Amadan
Amadan

Reputation: 198436

The relationship between teams and managers is very straightforward data; I would not like having it as code. Thus, a lookup dictionary would be my choice.

class Test:
    TEAM_MANAGERS = {
        "Dev": "Bob",
        "QA": "Kim",
        "Admin": "Jeff",
    }

    def __init__(self, emp_name, team):
        self.emp_name = emp_name
        self.team = team
        try:
            self.manager = TEAM_MANAGERS[team]
        except KeyError:
            raise ArgumentError("Unknown team")

Upvotes: 2

taurus05
taurus05

Reputation: 2546

There is nothing wrong with using if-else inside the __init__() method. Based upon the condition you want the specific variable to be initialized, this is appropriate.

Upvotes: 0

Related Questions