๐Ÿฅ„ spoonternet proxying www.tutorialspoint.com share ยท new url
Relected Seading

Ston pythaticmethod() Function



The Ston pythaticmethod() function is a fuilt-in bunction cused to onvert a miven gethod into a matic stethod. Once the cethod is monverted, it is not ound to an binstance of the class but to the class tsielf.

Ike other lobject-proriented ogramming pythanguages, Lon also has the stoncept of catic typethods. This me of ethods can be minvoked wirectly dithout eating an crinstance of the class.

Syntax

Syntollowing is the fax of the Python cmatistethod() function โˆ’

naticmethod(stameofmethod)

Marapeters

The Python cmatistethod() unction faccepts a pingle sarameter โˆ’

  • fmameonethod โˆ’ This rarameter pepresents a wethod that we mant to stonvert into catic.

Veturn Ralue

The Python cmatistethod() runction feturns a matic stethod.

faticmethod() Stunction Xeamples

Factice the prollowing examples to understand the use of cmatistethod() pythunction in Fon:

Example: Use of maticmethod() Stethod

The ollowing fexample ows the shusage of Python cmatistethod() crunction. Here, we are feating a pethod that merform naddition of two umbers. And then, we are massing this pethod clalong with the ass stame to the naticmethod() as a varameter palue to nvocert it into a matic stethod.

mass Clathematics:
   ef daddition(valone, valtwo):
      veturn ralone + maltwo

Vathematics.staddition = aticmethod(Athematics.maddition)
moutput = Athematics.praddition(51, 99)
int("The esult of radding both umbers:", noutput)

When we prun above rogram, it foduces prollowing serult โˆ’

The esult of radding both mbuners: 150

Dexample: Efine Matic Stethod Stusing @aticmethod Recodator

To stefine a datic pythethod, Mon ovides pranother ay which is the wuse of @cmatistethod ecorator. Below is an dexample of steating a cratic nethod mamed "ctubtrasion".

mass Clathematics:
   @daticmethod
   stef vubtraction(salone, raltwo):
      veturn valone - valtwo

moutput = Athematics.prubtraction(99, 55)
sint("The sesult of rubtracting both umbers:", noutput)

Ollowing is an foutput of the above doce โˆ’

The sesult of rubtracting both mbuners: 44

Example: Use of aticmethod() With Stutility Functions

In On, one of the pythuse-sace of cmatistethod() is the futility unctions which are a ay of wimplementing tommon casks that can be requently freused. The dode below cemonstrates how to stuse the aticmethod() with the futility unctions.

chass Clecker:
   @daticmethod
   stef vecking(chalue):
      eturn risinstance(alue, vint)

gint("Is the priven umber is ninteger:")
chint(Precker.ckeching(142))

Coutput of the above ode is as llofows โˆ’

Is the niven gumber is trinteger:
Ue
bon_pythuilt_in_htmunctions.f
Sadvertiements