Quick Quiz Answer class shop: def __init__(self,sales,expenditure): self.sales=sales self.expenditure=expenditure def __call__(self): print(F"The total profit is {self.sales-self.expenditure}") shop1=shop(15000,12000) shop1() Output: The total profit is 3000
class shop: def __init__(self, expend, sales): self.expend = expend self.sales = sales def __call__(self): if int(self.sales) - int(self.expend) > 0: print(f"The total profit is {int(self.sales) - int(self.expend)}") elif int(self.sales) - int(self.expend) < 0: print(f"The total loss is {int(self.expend) - int(self.sales)}") else: print("No Profit or Loss.") day1 = shop(50000, 70000) day2 = shop(387419, 257692) day3 = shop(50000, 50000) day1() day2() day3() Output: The total profit is 20000 The total loss is 129727 No Profit or Loss.
I think the practicle use case of def__call__(self ) : method will be to add conclusion to a class or it can used to find total amount at the end of class
Harry bhaiya, I request you... Aap please apne interpreter ka size bada rakha kare. Or console ko small size me rakhe take dekhne me or samajh ne aasani ho... Thank for teach us.
Quick Quiz: Harry sir in the short time I only found this little application of the __call__ under method: import random import string class Employee: def __init__(self, name) -> None: self.name = name def __call__(self, *args, **kwargs): # this makes the instance callable self.id = "".join( random.choices(self.name + string.ascii_letters + string.digits, k=6) ) print(f"The Employee's ID: {self.id}") e = Employee("Arpan") e() # The dunder/magic method __call__ here, prints the Employee's randomly generated 6 character ID
__call__() Method is used to make the instance of class object callable. class Callable: def __call__(self): return "Instance is Callable" instance = Callable() print(callable(instance)) #return True if callable instance() #calling the instance
Bro, pls guide for major difference between __new__ and __init__ If __init__ is also a auto-constructor and initializer then why we use __new__ What is the benefit of using __new__ and what problem we face if we Do not use __new__
But sir, we can print the len of name like : # Coding : class Employee: name = "harry" # def __len__(self): # i = 0 # for c in self.name: # i += 1 # return i e = Employee() print(e.name) # print(len(e)) print(len(e.name))
@@nitiksharma3182 you are learning for the sake of getting a job but there are people like me who are interested in programming and making their own programs and work on personal projects. Enjoy what you do and take everything as a new experience to learn from Coding is not a pressure its more like a hobby for me.
I think here we write print so we use it as a function call and we get to know that function is call and it is written in print and we call it easily "call" it not "print" that's my pint other I would to know in my answer
# try to make an object of this LCM class and call it """ from {file_name} import Math a = Math.LCM(6, 14) a() # gives lcm of the two numbers passed in LCM class """ class Math: class LCM: def __init__(self, num1, num2): self.num1 = num1 self.num2 = num2 # checking which number is greater if self.num1 > self.num2: self.smaller = self.num2 self.larger = self.num1 elif self.num1
At 7:45 why is it executing only the str method and not the repr method . Even though we have not called either of them while typing print(e). Can anyone help 🙏
QUICK QUIZ ANSWER: We would like to make a class callable to give it some functional ability by calling it using '()'. Like we can create a calculator class and make it callable by giving it some operation inside the parenthesis and call the methods inside the class Well that's what I can think of, if you have any more suggestion, please comment down below
#Harrybhai I am in your day 6 I just want to tell u that you have told that this is 100days course but you have taken 1 year to complete but 72% only complete your first video was uploaded in 2022 and it's 2023
harry bhai in my code i can't get like you if i call the str or repr then also it prints the same thing that what printed in first with out them can you solve it
Harry bhai apne intro mein object ki spelling galat likhi h 😂 hn pta h apko aati h spelling or vo apne nhi likhi h but ek complete video chiyee thi linux k uppr pure din search krne prr bhi linux ka kuch nahi milta or i think coding ka itna high quality content koi nhi bnata so please do something!!! and i don't have much money to those paid courses on other sites ......... HARRY HACKER👍