TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to PYTHON

Python classmethod and staticmethod Use

Use the classmethod and staticmethod decorators on functions.
Classmethod, staticmethod. Consider a class. We create an instance of the class. But some features of the class may not require an instance—they are more general-purpose.Class
This is where a static method is needed. A static method (indicated with "@staticmethod") means the method is called on the type name, not an instance.Built-ins
Classmethod. This is a function decorator. We apply it by specifying @classmethod before "def." It is a combination of an instance, and static, method. It can be called either way.

So: We can call the classmethod example, with the syntax Box.example or on a Box instance "b."

Class: The class argument ("cls" here) can be used as to create a type and return it. Or we can ignore it.

Python program that uses classmethod class Box: @classmethod def example(cls, code): # This method can be used as an instance or static method. print("Method called:", code) # Use classmethod as a static method. Box.example("cat") # Use classmethod as an instance method. b = Box() b.example("dog") Output Method called: cat Method called: dog
Staticmethod. A static method accepts no self instance. Most methods in a class accept a first argument with name "self." With the @staticmethod decorator, though, we omit this argument.

Tip: Static methods can return any value. They can accept any arguments. They are called with the class name, or on an instance.

Also: It makes no difference whether you call a static method with Box.Message or on an instance like b.Message.

Python program that uses staticmethod class Box: @staticmethod def Message(a): print("Box Message", a) # Call static method with type. Box.Message(1) # Call static method with instance. b = Box() b.Message(2) Output Box Message 1 Box Message 2
A review. Class methods and static methods are useful in Python programs. Often a class has parts to it that are not instance-based. Requiring an instance would be cumbersome and awkward.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf