Timothy Lombard Timothy Lombard - 1 year ago 34
Python Question

Can I call a dict of python objects from with a method of another class?

I've created a class called Emp has attributes like employee number, department number, employee type etc. I've used this class to populate a dictionary called emp_dict.

I've done a similar thing for Dept with details of the department, now I want to create a method in the department class to measure the number contractors in this class but do do this I reference the emp_dict.

class Dept():
def __init__(self,deptno, deptname):
self.deptno = deptno
def contractor_count(self,qtr,emp_dict):
contractor_count = 0
if qtr in ['FY14Q1A','FY14Q2A','FY14Q3A','FY14Q4A',\
for worker in self.allo[qtr].keys():
if emp_dict[worker].emptype != "R" :
contractor_count_= 1
return contractor_count

When I tried using the count_contractor method it returns the initial value: 0
Should I be using another approach?

wim wim
Answer Source

The contractor_count variable is never touched again. You have:

contractor_count_= 1

Did you mean this?

contractor_count += 1

I would also recommend using a variable name that is different from the method name, to avoid possible confusion.