Result of the __init__(self) related method is not showing up - python

class Employee(object):
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '#company.com'
def fullname(self):
return '{} {}'.format(self.first, self.last)
def pay_raise(self):
self.pay = int(self.pay * 1.04)
emp_1 = Employee("Mark", "Johnson", 50000)
emp_1.pay_raise()
When I write that emp_1.pay_raise() phrase or emp_1.fullname() i dont get any results or any errors either after pressing "run" or "debug" in pycharm. Can you notice any mistakes in my code? I will very appereciate.

You are not printing out any of the "results" from executing the methods. Try this:
class Employee(object):
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '#company.com'
def fullname(self):
return '{} {}'.format(self.first, self.last)
def pay_raise(self):
self.pay = int(self.pay * 1.04)
emp_1 = Employee("Mark", "Johnson", 50000)
print(emp_1.fullname())
emp_1.pay_raise()
print(emp_1.pay)

Related

How to use class attribute as instance attribute in Python? [duplicate]

This question already has answers here:
python class attribute
(3 answers)
List as class attribute
(2 answers)
Closed 1 year ago.
I am new to Python programming and I was coding the following script to increase my understanding of Pyhton OOP.
# Python OOP
class Employee:
num_of_emps = 0
raise_amt = 1.04
def __init__(self, first_name, last_name, pay):
self.first_name = first_name
self.last_name = last_name
self.pay = pay
self.email = first_name.lower() + '.' + last_name.lower() + '#company.com'
Employee.num_of_emps += 1
def fullname(self):
return self.first + ' ' + self.last
def apply_raise(self):
self.pay = int(self.pay * self.raise_amt)
#classmethod
def from_string(cls, emp_str):
name, surname, pay = emp_str.split('-')
return cls(name, surname, pay)
class Developer(Employee):
raise_amt = 1.01 # overriding raise_amt
def __init__(self, first_name, last_name, pay, prog_lang=None):
super().__init__(first_name, last_name, pay)
self.prog_lang = prog_lang
#classmethod
def with_prog_lang(cls, first_name, last_name, pay, prog_lang):
return cls(first_name, last_name, pay, prog_lang)
#classmethod
def from_string_kebab_case(cls, emp_str):
first_name, last_name, pay, prog_lang = emp_str.split('-')
return cls(first_name, last_name, pay, prog_lang)
class Manager(Employee):
raise_amt = 1.1
supervised_employee = []
def __init__(self, first_name, last_name, pay, employees=None):
super().__init__(first_name, last_name, pay)
if employees is not None:
self.supervised_employee.append(employees)
def add_emp(self, employee):
if employee not in self.supervised_employee:
self.supervised_employee.append(employee)
def remove_emp(self, employee):
if employee in self.supervised_employee:
self.supervised_employee.pop()
def show_employees(self):
if (len(self.supervised_employee) == 0):
print(f"{self.first_name} is managing no one")
else:
print(f"{self.first_name} is managing ", end='')
output = ''
for emp in self.supervised_employee:
output = output + emp + ' ,'
print(f"{output}")
emp1 = Developer('Burak', 'Aksoy', 5000)
manager1 = Manager('Ahmet', 'Nazli', 10000, 'Burak')
manager1.add_emp('John')
manager2 = Manager('Faruk', 'Tuncer', 10000)
print(manager1.supervised_employee)
manager1.show_employees()
manager2.show_employees()
Here, I have the following output as I run the code ->
{'first_name': 'Ahmet', 'last_name': 'Nazli', 'pay': 10000, 'email': 'ahmet.nazli#company.com'}
['Burak', 'Harun']
Ahmet is managing Burak ,John ,
Faruk is managing Burak ,John ,
When I add 'Burak' and 'John' as employees to manager1, I see that manager2 is also affected by this, but I don't want this to happen.. How do you think I can fix this?
Any help is appreciated.
Best.

Issue printing out __str__() function for location of a list type object

When printing out the str() function for the Manager() class the employees list prints out the location of the objects in memory instead of in type(str) format. The rest of the Manager str prints out fine, but I can't figure out how to get the employees list to print out. I'm just using the GUI to test aspects of the code for now.
Thank you in advance for your time.
Image of incorrect output on GUI
import random
from random import randint
from tkinter import *
import tkinter as tk
from tkinter import ttk
class Employees(object):
'''
'''
num_emps = 0
def __init__(self, first_name, last_name, pay, emp_id):
self.first_name = first_name
self.last_name = last_name
self.pay = pay
self.emp_id = self.IdMaker()
Employees.num_emps += 1
def get_first_name(self): #Retrieves First Name
return self.first_name
def get_last_name(self): #Retrieves Last Name
return self.last_name
def get_full_name(self): #Retrieves Full Name
return '{} {}'.format(self.first_name, self.last_name)
def __str__(self):
return '{self.emp_id}: {self.first_name} {self.last_name}'.format(self=self)
#####---ID CLASS---#####
class IdMaker():
'''
'''
def __init__(self):
self.emp_id = 'emp_' + ''.join(str(random.randint(0, 9)) for x in range(12))
self.food_id = 'food_' + ''.join(str(random.randint(0, 9)) for x in range(12))
self.sanitation_id = 'sani_' + ''.join(str(random.randint(0, 9)) for x in range(12))
self.dinnerWare_id = 'dWare_' + ''.join(str(random.randint(0, 9)) for x in range(12))
def __str__(self):
if isinstance(self.emp_id, object):
return '{self.emp_id}'.format(self=self)
elif isinstance(self.food_id, object):
return 'food_{self.food_id}'.format(self=self)
elif isinstance(self.sanitation_id, object):
return 'sani_{self.sanitation_id}'.format(self=self)
elif isinstance(self.dinnerWare_id, object):
return 'sani_{self.dinnerWare_id}'.format(self=self)
else:
print('No such object')
#-----------------------------------------------------------------------------
class Manager(Employees):
def __init__(self, first_name, last_name, pay, emp_id, employees=None):
super().__init__(first_name, last_name, pay, emp_id)
# if schedule is None: #Schedule
# self.schedule = {}
# else:
# self.schedule = schedule
if employees is None: #Employees working for Manager
self.employees = []
else:
self.employees = employees
def add_emp(self, emp):
if emp not in self.employees:
self.employees.append(emp)
def remove_emp(self, emp):
if emp in self.employees:
self.employees.remove(emp)
def print_emps(self):
for emp in self.employees:
print(emp.get_full_name())
def __str__(self):
return '{self.emp_id}: {self.first_name} {self.last_name} |Pay - ${self.pay} - {self.employees}'.format(self=self)
class FOH(Manager):
def __init__(self, first_name, last_name, pay, emp_id):
Employees.__init__(self, first_name, last_name, pay, emp_id)
self.pay = pay
# if schedule is None: #Schedule
# self.schedule = {}
# else:
# self.schedule = schedule
def __str__(self):
return '{self.emp_id}: {self.first_name} {self.last_name} |Pay - ${self.pay}'.format(self=self)
class BOH(Manager):
def __init__(self, first_name, last_name, pay, emp_id):
Employees.__init__(self, first_name, last_name, pay, emp_id)
self.pay = pay
# if schedule is None: #Schedule
# self.schedule = {}
# else:
# self.schedule = schedule
def __str__(self):
return '{self.emp_id}: {self.first_name} {self.last_name} |Pay - ${self.pay}'.format(self=self)
foh_1 = FOH('Rebecca', 'Peters', 60000, None)
foh_2 = FOH('Becca', 'Peters', 60000, None)
boh_3 = BOH('Beckles', 'Peters', 60000, None)
mgr_1 = Manager('Sean', 'Sheaffer', 90000, None, [foh_1, foh_2, boh_3])
# mgr_1.add_emp([foh_2])
# mgr_1.add_emp([boh_3])
mgr_2 = Manager('Becky', 'Peters', 100000, None, [boh_3])
mgr_2.print_emps()
mgr_1.print_emps()
#####---GUI CLASS---#####
root = Tk()
root.title('DigiSous')
root.geometry("600x200")
# Creates tab control
tabControl = ttk.Notebook(root)
class GUI(Manager):
def __init__(self, master, first_name, last_name, pay, emp_id):
super().__init__(first_name, last_name, pay, emp_id)
my_frame = Frame(master)
my_frame.pack()
#Find Employees working for Manager
self.find_mgr_emps = Button(master, text="Employees", command=self.manager_emps)
self.find_mgr_emps.pack(pady=20)
self.find_mgr_emps_Entry = Entry(master, width=90, borderwidth=5)
self.find_mgr_emps_Entry.pack(pady=10)
def manager_emps(self):
self.find_mgr_emps_Entry.delete(0, END)
mgr_emps = mgr_1
self.find_mgr_emps_Entry.insert(0, mgr_emps)
#-----------------------------------------------------------------------------
e = GUI(root, None, None, None, None)
root.mainloop()
Whenever you print a list, you will see the __repr__ of those objects instead of __str__, so one way could be to override the __repr__ method to get your desired output, as for example:
def __repr__(self):
return str(self)
Another way could be to use a list comprehension when you format your string as follows:
class Manager(Employees):
def __str__(self):
return '{self.emp_id}: {self.first_name} {self.last_name} |Pay - ${self.pay} - {[str(x) for x in self.employees]}'.format(self=self)

How to pass an integer through an instance method and add that with an instance variable?

I was trying to add the parameter bonus (which will take an integer) with the instance variable self.pay and wanted to print that final payment with the worker's name. But, I could not print that added total payment
I want to call the method rise() instead of returning anything from it, but I am confused how I can call that and pass an integer number.
class Information:
def __init__(self,first,last,pay):
self.first = first
self.last = last
self.pay = pay
def rise(self,int(bonus)):
self.pay = self.pay + bonus
def __str__(self):
return "%s and %s and has a balance of %s" % (self.first,self.last,self.pay)
if __name__ == "__main__":
emp1 = Information("tom","jerry",999)
print (emp1)
class Information:
def __init__(self,first,last,pay):
self.first = first
self.last = last
self.pay = pay
def raise_salary(self, bonus):
self.pay += int(bonus) # exception if bonus cannot be casted
def __str__(self):
return "%s and %s and has a balance of %s" % (self.first,self.last,self.pay)
if __name__ == "__main__":
emp1 = Information("tom", "jerry", 999)
print(emp1)
emp1.raise_salary('1000') # or just emp1.raise(1000)
print(emp1)
I tried with below code.
I updated def rise(self,int(bonus)): to def rise(self,bonus):
class Information:
def __init__(self,first,last,pay):
self.first = first
self.last = last
self.pay = pay
def rise(self,bonus):
self.pay = self.pay + bonus
def __str__(self):
return "%s and %s and has a balance of %s" % (self.first,self.last,self.pay)
if __name__ == "__main__":
emp1 = Information("tom","jerry",999)
emp1.rise(89)
print (emp1)

TypeError: __str__ returned non-string (type NoneType)

So for my last assingment in my python course at uni, I have to write a program consisting of three objects, two of which inherit. I keep running into a snag especially with regards to the last two objects. Here is my code:
class Course:
def __init__(self,title="",ID=0):
self._ID = ID
self._title = title
def getID(self):
return self._ID
def getTitle(self):
return self._title
def setTitle(self,title):
self._title = title
def setID(self,ID):
self._ID = ID
def __repr__(self):
return "Title: " + self._title + "ID: " + str(self._ID)
class OfferedCourse(Course):
def __init__(self,title="",ID=0,enrollment=[]):
super().__init__(title,ID)
self._enrollment = len(enrollment)
def getEnrollment(self):
return self._enrollment
def addStudent(self,stu):
if stu in enrollment:
print("Student is already enrolled.")
else:
enrollment.append(stu)
def dropStudent(self,stu):
if stu in enrollment:
def __repr__(self):
super().__repr__() + "Enrollment: " + str(self._enrollment)
class StudentCourse(Course):
def __init__(self,grade,ID=0,title=""):
super().__init__(title,ID)
self._grade = grade
def getGrade(self):
return self._grade
def setGrade(self,grade):
self._grade = grade
def __repr__(self):
super().__repr__() + "Grade: " + str(self._grade)
def main():
#Set primary course
lego=Course("Lego Design",32013)
#display course
print(lego)
#Set OfferedCourse
bonk=OfferedCourse("Matoran History",82932,["Josh","Rick","Greg","Chris"])
#Display OfferedCourse
print(bonk)
#Set StudentCourse
lp=StudentCourse("History of Nu-Metal",57859,82)
#display Student Course
print(lp)
At around line 60 I recieve the error:
TypeError: str returned non-string (type NoneType)
I'm pretty lost as to what is going on.
Your __repr__s don't explicitly return anything. You build up a string, then throw it away, causing None to be implicitly returned instead.
Just add a return:
def __repr__(self):
return super().__repr__() + "Grade: " + str(self._grade)
Adjustments to the source code of the original question:
add missing statement at def dropStudent(self,stu):
add missing return expression for def __repr__(self):
adjust signature of StudentCourse(Course) init to def __init__(self,title,ID,grade): to be in line with parent classes and process given statement StudentCourse("History of Nu-Metal",57859,82) as expected
add missing indentions for def main():
class Course:
def __init__(self,title="",ID=0):
self._ID = ID
self._title = title
def getID(self):
return self._ID
def getTitle(self):
return self._title
def setTitle(self,title):
self._title = title
def setID(self,ID):
self._ID = ID
def __repr__(self):
return "Title: " + self._title + "ID: " + str(self._ID)
class OfferedCourse(Course):
def __init__(self,title="",ID=0,enrollment=[]):
super().__init__(title,ID)
self._enrollment = len(enrollment)
def getEnrollment(self):
return self._enrollment
def addStudent(self,stu):
if stu in enrollment:
print("Student is already enrolled.")
else:
enrollment.append(stu)
def dropStudent(self,stu):
if stu in enrollment:
print("#todo Something is missing here...")
def __repr__(self):
return super().__repr__() + "Enrollment: " + str(self._enrollment)
class StudentCourse(Course):
def __init__(self,title,ID,grade):
super().__init__(title,ID)
self._grade = grade
def getGrade(self):
return self._grade
def setGrade(self,grade):
self._grade = grade
def __repr__(self):
return super().__repr__() + "Grade: " + str(self._grade)
def main():
#Set primary course
lego=Course("Lego Design",32013)
#display course
print(lego)
#Set OfferedCourse
bonk=OfferedCourse("Matoran History",82932,["Josh","Rick","Greg","Chris"])
#Display OfferedCourse
print(bonk)
#Set StudentCourse
lp=StudentCourse("History of Nu-Metal",57859,82)
#display Student Course
print(lp)
main()

Trying Python 3 code for this simple Employee object and I'm stuck

Here's the simple python 3 object code from the web that is not platform dependent.. I cannot get working
class Employee:
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '#company.com'
def fullname(self):
return '{}{}'.format(self.first, self.last)
emp_1 = Employee('John','Doe','80000')
emp_2 = Employee('Jane','Foo','90000')
emp_2.fullname()
print (Employee.fullname(emp_1))
print (emp_2.fullname())
The error I get is as follows:
NameError Traceback (most recent call last)
in ()
----> 1 class Employee:
2
3 def init(self, first, last, pay):
4 self.first = first
5 self.last = last
in Employee()
10 return '{}{}'.format(self.first, self.last)
11
---> 12 emp_1 = Employee('John','Doe','80000')
13 emp_2 = Employee('Jane','Foo','90000')
14
NameError: name 'Employee' is not defined
Indentation is crucial in Python. Try the below code.
Your class instances must be defined outside the class itself. This is recognised by there being no indentation for definitions of emp_1 and emp_2.
class Employee:
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '#company.com'
def fullname(self):
return '{}{}'.format(self.first, self.last)
emp_1 = Employee('John','Doe','80000')
emp_2 = Employee('Jane','Foo','90000')
emp_2.fullname()
print(Employee.fullname(emp_1))
print(emp_2.fullname())
This is simply an indentation error.
Python defines scopes like classes, methods and other blocks by indentation. Usually 4 spaces are used.
Since you put your instantiation of emp_1 and emp_2 with the same indentation as the class's methods they are literally part of the class.
What you probably meant was:
class Employee:
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '#company.com'
def fullname(self):
return '{}{}'.format(self.first, self.last)
emp_1 = Employee('John','Doe','80000')
emp_2 = Employee('Jane','Foo','90000')
emp_2.fullname()
print (Employee.fullname(emp_1))
print (emp_2.fullname())
class Employee:
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '#company.com'
def fullname(self):
return '{}{}'.format(self.first, self.last)
def main():
emp_1 = Employee('John','Doe','80000')
emp_2 = Employee('Jane','Foo','90000')
emp_2.fullname()
print (Employee.fullname(emp_1))
print (emp_2.fullname())
if __name__ == '__main__':
main()

Categories

Resources