- 4
What does statitc method means?
2 ответов
+ 2
OK, so a static method is a method that doesn't need any additional arguments when it's called. and in its definition doesn't include the self argument and has on top of it a static decorator "@staticmethod". what it basically means that the static method behave the same way in all the object created by the class. usually it's used as a verification or assertion method.the static method is called from the class. example:
class car_class:
def __init__(self,car_tuple):
x,y,z=car_tuple
self.model=x
self.color=y
self.fuel=z
@staticmethod
def start(car_tuple):
x,y,z=car_tuple
print "trying to start the "+y+" "+x
if z==0:
print "tank empty. Car wouldn't start"
else:
print " The engine is roaring. Car started"
return true
Now the start method is called like this:
Cars=[('mazda','black',0),('bmw','white',70)]
running_car=[]
for test_car in Cars:
if car_class.start(test_car): #staticmethod from class
running_car.append(test_car)
print running_car
output:
>>>
trying to start the black mazda
tank empty. car wouldn't start
trying to start the white BMW
the engine is roaring. car started
[('bmw','white,70)]
>>>
+ 1
thank you freind 😉