Method as an argument with self parameter - python

Disregard the naming conventions as to what class is part of which it is just a test run.
I need some help with the OOP inheritance i have created a class of Students, Teachers, and the principal. My goal is for the principal to be able to add employees. The problem is i just want to use a for loop to get the names and then pass that method as an attribute for the principal object. i was able to do it with the class Input without the self parameter. can someone tell me
what is going on here and how can i fix this with self. i removed input from names so that my question wont get shut down
class Input:
def count():
cnt = []
for i in range(4):
name = ('Enter name here: ')
cnt.append(name)
return cnt
class Student:
def __init__(self,name,lastname):
self.name = name
self.lastname = lastname
class StudentCouncil(Student):
def __init__(self, name, lastname, tenure):
super().__init__(name,lastname)
self.tenure = tenure
class Principal(StudentCouncil):
def __init__(self, name, lastname, tenure,employees=None):
super().__init__(name,lastname,tenure)
if employees is None:
self.employees = []
else:
self.employees = employees
def display(self):
for names in self.employees:
print(names,end=' ')
count = Input.count()
tij = Principal('Mike','Thoma','3',count)
tij.display()

If the method takes a self parameter, you need to create an instance of the class. So it would be:
class Input:
def count(self):
cnt = []
for i in range(4):
name = input('Enter name here: ')
cnt.append(name)
return cnt
and then you would do:
myinput = Input()
count = myinput.count()
Your count() method doesn't use any attributes of self, so it doesn't currently need to be written this way. But you might want to redefine it like this:
class Input:
def __init__(self, howmany):
self.howmany = howman
def count(self):
return [input('Enter name here: ') for _ in range(self.howmany)]
myinput = Input(4)
count = myinput.count()

If count is all you want from Input, just make it a function:
def input_names():
cnt = []
for i in range(4):
name = ('Enter name here: ')
cnt.append(name)
return cnt
If you want a configurable Input type of some sort, then you want to run count on an instance of that, you need self:
class Input:
def count(self):
cnt = []
for i in range(self.num_names): # if we need some configuration
name = ('Enter name here: ')
cnt.append(name)
return cnt
Else, the kosher way to do this is to use the staticmethod decorator:
class Input:
#staticmethod
def count():
cnt = []
for i in range(4):
name = ('Enter name here: ')
cnt.append(name)
return cnt
Your current code will work as you use it currently, Input.count(), but if you instantiated an input, Input().count() would throw an exception. The staticmethod decorator ensures that this method is safe to call on either the class directly or on an instance of that class.

Related

How do i access all values of a particular instance variable in python class

I'm practicing OOP in python and I'm trying to rewrite my code using class. Each instance in the class is meant to have a unique value for a particular instance variable. So I need to check to see if the value that is to be assigned is not being used by another instance before assigning.
So for example, how do i convert something like this using class.
from random import randint
accounts = {}
acc_number = []
names = ['john','ambrose','jess']
for name in names:
acc_num = randint(1,10)
while True:
if acc_num in acc_number:
acc_num = randint(1,10)
else:
acc_number.append(acc_num)
break
accounts[name] = acc_num
print(accounts)
Since the purpose of class is to keep each instance's values apart, how can I neatly ensure acc_numberis unique?
are you talking about how to make a class that's guaranteed to have a unique attribute named acc_num?
used_acc_nums = []
class Account:
def __init__(self,name):
self.name = name
self.acc_num = random.randint(0,10)
while self.acc_num in used_acc_nums:
self.acc_num = random.randint(0,10)
used_acc_nums.append(self.acc_num)
john = Account("John")
ambrose = Account("Ambrose")
jess = Account("Jess")
for acc in [john, ambrose,jess]:
print(acc.name , acc.acc_num)
Could have something like this:
from random import choice
class Person:
def __init__(self):
self.id = list(range(1, 10))
self.accounts = {}
def assign(self, name):
get_id = choice(self.id)
self.id.remove(get_id)
self.accounts[name] = get_id
def accounts(self):
return self.accounts
person = Person()
people = ['john', 'ambrose', 'jess']
for p in people:
person.assign(p)
print(person.accounts)
>>> {'john': 6, 'ambrose': 8, 'jess': 7}

Incomprehensible input behavior

There was a problem entering the fields of class. At the time of calling the add_well function, the default class fields specified in init function are displayed in the console. I don't understand why this behavior occurs and how to get rid of it. Can you help me?
def InputConsole(self, param):
return input(param)
#class of well
class well:
def __init__(self):
self.code_name = ''
self.depth = 0
self.pressure = 0
self.work = False
def input_properties(self):
print("-------- ADD WELL --------")
print('Code name: ')
self.code_name = InputConsole(self, self.code_name)
print('Depth: ')
self.depth = InputConsole(self, self.depth)
print('Pressure: ')
self.pressure = InputConsole(self, self.pressure)
print('Work: ')
self.work = InputConsole(self, self.work)
#class of field, container class
class field():
def __init__(self):
self.field = dict()
self.id_max = 0
def add_well(self):
wl = well()
wl.input_properties()
self.field[self.id_max] = wl
self.id_max += 1
print('Well was added\n\n')
def print_console(self):
print(self.field)
if self.id_max != 0 :
print("-------- INFO ABOUT FIELD --------")
for i in range(self.id_max):
self.field[i].print_properties()
print('')
else:
print('List is empty\n\n')
def main():
field = field()
field.add_well()
main()
Calling input() with a parameter uses the parameter as a prompt. Change the calls of InputConsole(self, self.code_name) to InputConsole(self, “Enter code name: “) and so on.
In addition, there’s really no reason you should be making an InputConsole function. You should also not be using self as a parameter for some of these methods, because your InputConsole, add_well, and print_console functions are not members of a class.

Python project help (classes/expected type)

I am working on a project for school, simulating a payroll program, and I am getting an error. The error I am getting is
'Expected type 'Classification', got 'Employee' instead'. The relevant code is (I put *** around the code generating the error, it is the 5th function under the Employee Class).
class Employee:
def __init__(self, emp_id, first_name, last_name, address, city, state, zipcode, clas = None):
self.emp_id = emp_id
self.first_name = first_name
self.last_name = last_name
self.address = address
self.city = city
self.state = state
self.zipcode = zipcode
self.classification = clas
def make_hourly(self, hourly_rate):
self.clas = Hourly(hourly_rate)
self.classification = self.clas
def make_salaried(self, salary):
self.clas = Salaried(salary)
self.classification = self.clas
def make_commissioned(self, salary, rate):
self.clas = Commissioned(rate, salary)
self.classification = self.clas
def issue_payment(self):
***pay = Classification.compute_pay(self)***
print('Mailing', pay, 'to', self.first_name, self.last_name, 'at', self.address, self.city, self.state, self.zipcode)
class Classification(ABC):
''' Interface for employee classifications '''
#abstractmethod
def compute_pay(self):
pass
class Hourly(Classification):
''' Manages timecard info. Computes pay '''
def __init__(self, hourly_rate):
self.hourly_rate = hourly_rate
self.timecards = [] # A list of floats representing hours worked
def compute_pay(self):
for i in list_of_timecards:
if i[0] == self.emp_id:
self.timecards.extend(i[1:])
total = list(map(float, self.timecards))
total = sum(total)
self.timecards.clear()
return total * self.hourly_rate
def add_timecard(self, hours):
self.timecards.append(hours)
class Salaried(Classification):
def __init__(self, salary):
self.salary = salary
def compute_pay(self):
return self.salary / 24
class Commissioned(Salaried):
def __init__(self, salary, commission_rate):
self.commission_rate = commission_rate
self.salary = salary
self.receipts = []
def add_receipt(self, amount):
self.receipts.append(amount)
def compute_pay(self):
for i in list_of_receipts:
if i[0] == self.emp_id:
self.receipts.extend(i[1:])
total = list(map(float, self.receipts))
total = sum(total)
self.receipts.clear()
return (self.salary / 24) + ((self.commission_rate / 100) * total)
My understanding of the problem is that I need to pass my 'employee' object to the 'compute_pay' function, which then passes it to the relevant child class (hourly etc...) to run and return the result. I have tried changing
pay = Classification.compute_pay(self)
to
pay = Classification.compute_pay(self.clas)
however that returns error 'AttributeError: 'Employee' object has no attribute 'clas'
which makes no sense. Maybe it is that I am not assigning the employees to the class correctly?
The code for that is (it pulls from a CSV file, and it is pulling the data correctly and generating the class objects, I have checked)
def load_employees():
f = open("employees.csv")
f.readline() # skip header line
for line in f:
fields = line.strip().split(',')
emp = Employee(*fields[:7])
if fields[7] == '3':
clas = Hourly(fields[10]) # Need to define Hourly
emp.classification = clas
elif fields[7] == '2':
clas = Commissioned(fields[8], fields[9])
emp.classification = clas
elif fields[7] == '1':
clas = Salaried(fields[8])
emp.classification = clas
employees.append(emp)
I will figure out your line Classification.compute_pay(self):
Classification => the class Classification
compute_pay => class
method self => this = an Employee instance
pass means do nothing and is used to avoid unneccessary code.
Every class method has self as an argument to allow refering to this instance of the class.
To pass an argument (here your employee) use a parameter. Also implementing a method of the parent class overrides this method.
Every function compute_pay should have a second argument
def compute_pay(self, employee):
# do your stuff
And then you can use this line in issue_payment
pay = self.clas.compute_pay(self)
Two issues here,
Firstly, your Employee instance has two attributes: clas and classification. However, in your constructor, only classification is set.
def __init__(...
...
self.classification = clas
But self.clas is not set to anything. That's why you are getting that error 'Employee' object has no attribute 'clas'. It is only set when one of the make_hourly, make_salaried, or make_commissioned methods are invoked. So when you load the employees CSV, instead of manually creating the instance like you are doing here
clas = Hourly(fields[10])
you should be calling the method make_hourly on your emp instance, like so
emp.make_hourly(fields[10])
It's worth noting that fields[10] is terrible naming. Instead of unpacking all the fields at once, try to unpack them during the for loop:
for a, b, c, d in csv:
...
Secondly, this line of code is wrong in multiple ways
pay = Classification.compute_pay(self)
compute_pay is not a static function or a classmethod. So it shouldn't be called on the Classification class itself, but the Classification instance. This is what you stored in your self.clas attribute. So, compute_pay should be called on self.clas:
def issue_payment(self):
pay = self.clas.compute_pay()
...
In addition to that, when you call a method of a class from inside of another method in the same class, you don't ever need to pass the self argument. It is implied. So even if compute_pay was static or a class method, which it isn't, it would be called like so,
Classification.compute_pay()
Notice there is no self inside the parentheses. Similarly, when you call another method that is not static, self is never passed as an argument:
def my_method(self):
self.another_method()

Class object does not update empty list

I'm trying to solve this problem on my own. The problem asks to write a class for employee information, then ask user to input that information, and finally print the entered information.
I want to use two for loops, one for getting information and one for printing the information. Unfortunately, the printing for loop does not working.
class Employee:
def __init__(self, name, id_num, department, job):
self.__name = name
self.__id_num = id_num
self.__department = department
self.__job = job
# setters
def set_name(self,name):
self.__name = name
def set_id(self,id_num):
self.__id_num = id_num
def set_department(self,department):
self.__department = department
def set_job(self,job):
self.__job = job
# getters
def get_name(self):
return self.__name
def get_id(self):
return self.__id_num
def get_department(self):
return self.__department
def get_job(self):
return self.__job
def main():
employee_list = []
for i in range(2):
name = input('What is the name of the employee? ')
id_num = float(input('What is the ID number of the employee? '))
department = input('What is the department where the employee works? ')
job = input('What is the job title of the empoyee? ')
personnel = Employee(name,id_num,department,job)
employee_list.append(personnel)
return employee_list
for item in employee_list:
print(item.get_name())
print(item.get_id())
print(item.get_department())
print(item.get_job())
print()
main()
You need to remove the following line in your main() function:
return employee_list
It is causing your main to stop running without ever reaching the printing for loop.

Python Adding a List of Class in Another Class

I have a class called Question as per below
class Question:
q_count = 0
def __init__(self, s_id, q_id, question):
print("q count is ", Question.q_count)
self._s_id = s_id
self._question_text = question
self._answers = []
self._q_id = Question.q_count
def get_answers(self):
return self._answers
def set_answers(self,answers):
self._answers = answers
def add_answer(self, Answer()):
self._answers.append(Answer())
and I want it to have a list called _answers which is a list of Answer objects. where Answer is another type of class i have created.
class Answer:
def __init__(self, q_id, a_id):
self._q_id = q_id
self._a_id = a_id
# Subclass for multiple choice
class MC_Answer(Answer):
def __init__(self, q_id, a_id, answer_text):
Answer.__init__(self, q_id, a_id)
self._answer_text = answer_text
def get_answer_text(self):
return self._answer_text
def set_answer_text(self, a_id):
self._a_id = a_id;
In another part of my code, I am creating an instance of an MC_Answer object. I also have an instance of a question object. How do I append this onto its answers list??
answer1 = MC_Answer(idQ, 1, answer_text)
new_q.add_answer(answer1)
write_to_file(alist1, "answers.csv")
this is wrong but if i don't have answer1 as a parameter, how does Python know to add that particular instance of an answer?
Your add answer class never uses the parameter, instead you make a new instance of the Answer class and append that to the list, instead you need to modify it to give it a parameter and then use it correctly
def add_answer(self, Answer()):
self._answers.append(Answer())
should be
def add_answer(self, answer):
self._answers.append(answer)

Categories

Resources