0

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" 
2
  • Are self.Manger & self.Manager different variables? Or its a typo? Commented Jan 30, 2019 at 6:50
  • Its typo, I am corrected it Commented Jan 30, 2019 at 6:52

4 Answers 4

2

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")
Sign up to request clarification or add additional context in comments.

1 Comment

This answer is much more descriptive. It's better to go with this one.
1

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}')

Comments

0

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.

Comments

0
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())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.