+ 1
Oops!!! python
class Example: def __init__(self): self.__num=10 @staticmethod def display(): self.__num+=1 print(self.__num) obj=Example() obj.display() who got it...and how??
8 odpowiedzi
+ 6
ah sorry no, you just pass in vars
+ 6
just browse static methods !
https://realpython.com/instance-class-and-static-methods-demystified/
+ 3
Your not using the display method like a static method, but rather just a normal method.
class Example:
def __init__(self):
self.__num=10
def display(self):
self.__num+=1
print(self.__num)
obj=Example()
obj.display()
a static method belongs to the class without access to class or instance variables and an instance of that class is not needed. the self keyword refers to the current instance of that class, therefore it shouldn't be used in a true static method.
0
Can I use self in staticmethod?
0
wht?? I didn't get your point
0
What's the mistake I made here? ? is that the use of self ?
0
thqq
0
exactly. ..! This is the answer I need..Thank you