@#classmethod vs @#staticmethod vs "plain" methods
What's the difference?
class MyClass:
def method(self):
"""
Instance methods need a class instance and
can access the instance through self.
"""
return 'instance method called', self
@classmethod
def classmethod(cls):
"""
Class methods don't need a class instance.
They can't access the instance (self) but
they have access to the class itself via cls.
"""
return 'class method called', cls
@staticmethod
def staticmethod():
"""
Static methods don't have access to cls or self.
They work like regular functions but belong to
the class's namespace.
"""
return 'static method called'
# All methods types can be
# called on a class instance:
»> obj = MyClass()
»> obj.method()
('instance method called', <MyClass instance at 0x1019381b8>)
»> obj.classmethod()
('class method called', <class MyClass at 0x101a2f4c8>)
»> obj.staticmethod()
'static method called'
# Calling instance methods fails
# if we only have the class object:
»> MyClass.classmethod()
('class method called', <class MyClass at 0x101a2f4c8>)
»> MyClass.staticmethod()
'static method called'
»> MyClass.method()
TypeError:
"unbound method method() must be called with MyClass "
"instance as first argument (got nothing instead)"
# @classmethod vs @staticmethod vs "plain" methods
# What's the difference?
class MyClass:
def method(self):
"""
Instance methods need a class instance and
can access the instance through self.
"""
return 'instance method called', self
@classmethod
def classmethod(cls):
"""
Class methods don't need a class instance.
They can't access the instance (self) but
they have access to the class itself via cls.
"""
return 'class method called', cls
@staticmethod
def staticmethod():
"""
Static methods don't have access to cls or self.
They work like regular functions but belong to
the class's namespace.
"""
return 'static method called'
# All methods types can be
# called on a class instance:
»> obj = MyClass()
»> obj.method()
('instance method called', <MyClass instance at 0x1019381b8>)
»> obj.classmethod()
('class method called', <class MyClass at 0x101a2f4c8>)
»> obj.staticmethod()
'static method called'
# Calling instance methods fails
# if we only have the class object:
»> MyClass.classmethod()
('class method called', <class MyClass at 0x101a2f4c8>)
»> MyClass.staticmethod()
'static method called'
»> MyClass.method()
TypeError:
"unbound method method() must be called with MyClass "
"instance as first argument (got nothing instead)"
#classmethod#staticmethod
goo.gl/QkvD2L
#staticmethod
#Classmethod
use #super () in #classmethod:
# Compare your code
# adjusted to use _ _ name _ _ to illustrate the difference:
»> class SimpleGenerator(object):
... @classmethod
... def get_description(cls):
... return cls. _ _ name _ _
...
# without super()
»> class AdvancedGenerator(SimpleGenerator):
... @classmethod
... def get_description(cls):
... desc = SimpleGenerator.get_description()
... return desc + ' Advanced(tm)'
...
»> AdvancedGenerator.get_description()
'SimpleGenerator Advanced(tm)'
# and using super():
»> class AdvancedGenerator(SimpleGenerator):
... @classmethod
... def get_description(cls):
... desc = super(AdvancedGenerator, cls).get_description()
... return desc + ' Advanced(tm)'
...
»> AdvancedGenerator.get_description()
'AdvancedGenerator Advanced(tm)'
#AI#Artificial_Intelligence
#AJAX
#aiohttp
#Anaconda
#AngularJS
#API
#Atom
#AWS
#asyncio (#Asynchronous)
#audio
#automated_testing
#automation
#atexit
#BeeWare
#Big_Data
#bitcoin
#blockchain
#Bluemix
#Brython
#button
#Celery
#client
#class
#classmethod
#concurrency
#Coroutine
#cron
#CSS
#curl
#data_analysis
#data_mining
#data_processing
#database
#Deep_Learning#deep_learning
#Debian
#decorator
#deploy
#dict
#dispatch
#django
#django_cms
#Django_REST_Framework
#dropdownbox
#Docker
#event
#Firefox
#Flask
#form
#functions
#Generator
#GeoDjango
#git
#Google
#GPU
#GUI
#Gym
#host
#HTML
#httplib
#learn
#Image_processing
#intelligence
#input
#Instagram
#IOT
#iPython
#Jupyter
#lambda
#learn
#License
#Linux
#lists
#machine_learning
#Magenta
#map
#Matplotlib
#Metaprogramming
#Micro_services
#Micropython
#mind
#monitoring
#MongoDB
#modules
#Mozilla
#Multipart
#multi_touch_apps
#multiprocessing
#Nodes
#NoSQL
#numeric_computation
#numerical
#NumPy
#network
#neural_network
#OAuth
#object_serialization
#OCR
#overloading
#package
#parallel
#pipeline
#protocols
#PostGIS
#pyAudioAnalysis
#pycon
#Pyflakes
#PyInstaller
#PyPI
#PyQt
#PySide
#PyTorch
#pytest
#python
#Pyvideo_archives
#Qt
#Raspberry_Pi
#React
#Redis
#random
#request
#Regular_Expressions (#re)
#REST
#RSS
#satellite
#scikit_learn
#SciPy
#scrapy
#searching
#selectbox
#Selenium
#serialization
#server
#sessions
#single_responsibility_principle
#socket
#Spark
#str
#submit
#task
#telegram
#template
#TensorFlow
#test
#text_boxes
#text
#tuples
#unicode
#Universe
#Unix
#unit_test
#urllib
#upload
#uWSGI
#Web
#WSGI