Unit 5 - Lesson 2

51.1.1. Understanding the self variable

class Manager:
	
	# define init method with self, name, department, salary attributes
	def __init__(self,name,department,salary):
		self.name = name
		self.department = department
		self.salary = salary
name = input("name: ")
department = input("department: ")
salary = int(input("salary: "))
manager_1 = Manager(name, department, salary)
print('Manager details:', manager_1.name, manager_1.department, manager_1.salary)

51.1.2. Using self variable

class Car:
	def setDetails(self, model, regno):
		# write your code here
		self.model = model
		self.regno = regno
	def getModel(self):
		# write your code here
		return self.model
	def getRegno(self):
		# write your code here
		return self.regno

Hyundai = Car()
Maruthi = Car()
#Take details of the car as input from user. Write your code here
m1 = input("car1 model: ")
r1 = input("car1 regno: ")
m2 = input("car2 model: ")
r2 = input("car2 regno: ")
Hyundai.setDetails(m1,r1)
Hyundai.m = Hyundai.getModel()
Hyundai.r = Hyundai.getRegno()

Maruthi.setDetails(m2,r2)
Maruthi.m = Maruthi.getModel()
Maruthi.r = Maruthi.getRegno()

print("Hyundai car details:", Hyundai.m,Hyundai.r)
print("Maruthi car details:", Maruthi.m,Maruthi.r)

51.1.3. Writing a class using a self variable as method parameter

# write your code here
class Car:
	def setName(self,name):
		self.name = name
	def getName(self):
		return(self.name)
Honda = Car()
c = input("car name: ")
Honda.setName(c)
print("Honda car name:", Honda.getName())

51.1.4. Understanding the usage of self variable

(b) self should be the first argument in the parameter list of any method
(c) self is always a reference to the current instance

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.