BogoToBogo
  • Home
  • About
  • Big Data
  • Machine Learning
  • AngularJS
  • Python
  • C++
  • go
  • DevOps
  • Kubernetes
  • Algorithms
  • More...
    • Qt 5
    • Linux
    • FFmpeg
    • Matlab
    • Django 1.8
    • Ruby On Rails
    • HTML5 & CSS

class method vs static method 2021

python_logo




Bookmark and Share





bogotobogo.com site search:





Quick Note

Python has three types of methods (normal, class, and static).
It's confusing and sometimes we're wondering why do we need all of them.
Even, there are people saying we do not need static method at all and recommend not using it.

The normal and class method requires passing in the first argument:

  1. self for normal method
  2. cls for class method
  3. but a static method requires none to be passed

We can check the behavior of each method regarding the first argument each method takes:

class MyClass:
  def __init__(self):
    pass

  def normal_method(*args,**kwargs):
    print("normal_method({0},{1})".format(args,kwargs))

  @classmethod
  def class_method(*args,**kwargs):
    print("class_method({0},{1})".format(args,kwargs))

  @staticmethod
  def static_method(*args,**kwargs):
    print("static_method({0},{1})".format(args,kwargs))

obj = MyClass()

obj.normal_method()
obj.normal_method(1,2,a=3,b=4)

obj.class_method()
obj.class_method(1,2,a=3,b=4)

obj.static_method()
obj.static_method(1,2,a=3,b=4)    

normal_method((<__main__.MyClass object at 0x10d06c370>,),{})
normal_method((<__main__.MyClass object at 0x10d06c370>, 1, 2),{'a': 3, 'b': 4})
class_method((<class '__main__.MyClass'>,),{})
class_method((<class '__main__.MyClass'>, 1, 2),{'a': 3, 'b': 4})
static_method((),{})
static_method((1, 2),{'a': 3, 'b': 4})    

So, a static method doesn't have access to self or cls.
The static method works like normal function but somehow belongs to the class: static method usually does not use variables defined in the class but lots of the times we just want to put the method into class definition because it has logical link (loosely coupled) to the class.
It also gives us maintenance benefits by just being defined in the class.

The static methods are used to do some utility tasks, and class methods are used for factory methods. The factory methods can return class objects for different use cases.



Class Method Static Method
Takes cls (class) as first argument. Does not take any specific parameter.
Can access and modify the class state. Cannot access or modify the class state.
Takes the class as parameter to know about the state of that class. Does not know about class state and is used to do some utility tasks by taking some parameters.
@classmethod decorator is used. @staticmethod decorator is used.


Q: What statement about static methods is true?

  1. Static methods are called static because they always return None.
  2. Static methods can be bound to either a class or an instance of a class.
  3. Static methods serve mostly as utility methods or helper methods, since they can't access or modify a class's state.
  4. Static methods can access and modify the state of a class or an instance of a class.

Ans: #3






Methods

Method is just a function object created by def statement.

Method works in exactly the same way as a simple function. But there is one exception: a method's first argument always receives the instance object:

  1. simple method : defined outside of a class. This function can access class attributes by feeding instance arg.
    def outside_foo():
    

  2. instance method :
    def foo(self,)
    

  3. class method : if we need to use class attributes
    @classmethod
    def cfoo(cls,) 
    

  4. static method : do not have any info about the class
    @staticmethod
    def sfoo() 
    

They have more similarities than differences.

Most of the cases, we can do the equivalent tasks with just a normal function (defined outside of a class). But in terms of software design, clean code, and effectiveness (inner workings), there is a slight difference in their usages.

In this section, we will discuss the difference between the class method and the static method.

There is another one called instance method but inner working of instance method is the same as the class method. Actually, Python automatically maps an instance method call to a class method. For instance, a call like this:

instance.method(args...)

will be automatically converted to a class method function call like this:

class.method(instance, args...)



Class Method and Instance Method

Suppose we have the following class with instance method foo():

# a.py
class A:
   message = "class message"

   @classmethod
   def cfoo(cls):
      print(cls.message)

   def foo(self, msg):
      self.message = msg
      print(self.message)

   def __str__(self):
      return self.message

Because the method foo() is designed to process instances, we normally call the method through instance:

>>> from a import A
>>> a = A()
>>> a.foo('instance call')
instance call

When we call the method, the Python automatically replace the self with the instance object, a, and then the msg gets the string passed at the call which is 'instance call'.


Methods may be called in two ways: the first one through an instance which we did above. Another way of calling is by going through the class name as shown below:

>>> A.foo('class call')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unbound method foo() must be called with A instance as first argument (got str instance instead)

We got the error because Python needs information about the instance. We did not provide anything about the instance. So, we should use the following form when we call a method through a class name as the error message suggested:

>>> A.foo(a, 'class call')
class call

Note that we pass the instance a as an argument of the foo() method.

Calls made through the instance and the class have the exact same effect, as long as we pass the same instance object ourselves in the class form.

For a class method, we can just simply call it through class name A:

>>> A.cfoo()
class message

Note:

  1. Bound method (instance call): To call the method we must provide an instance object explicitly as the first argument. In other words, a bound method object is a kind of object that remembers the self instance and the referenced function. So, a bound method may be called as a simple function without an instance later. Python automatically packages the instance with the function in the bound method object, so we don't need to pass an instance to cal the method. In other words, when calling a bound method object, Python provides an instance for us automatically-the instance used to create the bound method object. This means that bound method objects are usually interchangeable with simple function objects, and makes them especially useful for interfaces originally written for functions such as Callback functions.
  2. >>> class Callback:
    ...   def __init__(self, color):
    ...     self.color = color
    ...   def changeColor(self):
    ...     print(self.color)
    ... 
    >>> obj = Callback('red')
    >>> cb = obj.changeColor
    >>> cb()
    red
    

    Accessing a function attribute of a class by qualifying an instance returns a bound method object:
    >>> obj.changeColor
    <bound method Callback.changeColor of <__main__.Callback instance at 0x7f95ebe27f80>>
    

  3. Unbound method (class call): Accessing a function attribute of a class by qualifying the class returns an unbound method object:
    >>> Callback.changeColor
    <unbound method Callback.changeColor>
    

    To call the method, we must provide an instance object explicitly as the first argument:
    >>> obj2 = Callback('purple')
    >>> t = Callback.changeColor
    >>> t(obj2)
    purple
    

    The t is ab unbound method object (a function in 3.0), and we pass in instance.
    (Note): In Puython 3.0, the notion of unbound method has been dropped, and what we describe as an unbound method here is treated as a simple function in 3.0. For most purposes, this makes no difference to our code; either way, an instance will be passed to a method's first argument when it's called through an instance.]




Real world example of class method

The class method sometime used to define additional constructor. We can check it from cpython/Lib/datetime.py:

...
    # Additional constructors

    @classmethod
    def fromtimestamp(cls, t):
        "Construct a date from a POSIX timestamp (like time.time())."
        y, m, d, hh, mm, ss, weekday, jday, dst = _time.localtime(t)
        return cls(y, m, d)

    @classmethod
    def today(cls):
        "Construct a date from time.time()."
        t = _time.time()
        return cls.fromtimestamp(t)
...




Static Method

Static methods are used when we need to process data associated with classes instead of instances. A static method has no self argument and it is nested in a class and is designed to work on class attributes instead of instance attributes.

Static methods never receive an automatic self argument, whether called through a class or an instance. They usually keep track of information that spans all instances, rather than providing behavior for instances.

There have been some changes in the way how we define static method. Here, I do not want to discuss the variations from one Python version to another version, but just want to show a typical usage sample.

I'll use function decorator(@) for static method, and the syntax looks like this:

class S:
   @staticmethod
   def foo():
      ...

Internally, the definition has the same effect as the name rebinding:

class S:
   def foo():
      ...
   foo = staticmethod(foo)

Suppose we have a typical instance counting code like this:

# s.py
class S:
   nInstances = 0
   def __init__(self):
      S.nInstances = S.nInstances + 1

   @staticmethod
   def howManyInstances():
      print('Number of instances created: ', S.nInstances)

Then, we make three instances:

>>> from s import S
>>> 
>>> a = S()
>>> b = S()
>>> c = S()

Now that we have static method, we can call it in two ways:

  1. call from class
  2. call from instances
>>> S.howManyInstances()
('Number of instances created: ', 3)
>>> a.howManyInstances()
('Number of instances created: ', 3)




Instance, static, and class methods

Here is a simple code that has all types of methods:

class Methods:
  def i_method(self,x):
    print(self,x)

  def s_method(x):
    print(x)

  def c_method(cls,x):
    print(cls,x)

  s_method = staticmethod(s_method)
  c_method = classmethod(c_method)

obj = Methods()

obj.i_method(1)
Methods.i_method(obj, 2)

obj.s_method(3)
Methods.s_method(4)

obj.c_method(5)
Methods.c_method(6)

Output:

(<__main__.Methods instance at 0x7f7052d75950>, 1)
(<__main__.Methods instance at 0x7f7052d75950>, 2)
3
4
(<class __main__.Methods at 0x7f7052d6d598>, 5)
(<class __main__.Methods at 0x7f7052d6d598>, 6)

We get the same output from this code as well:

class Methods:
  def i_method(self,x):
    print(self,x)

  @staticmethod
  def s_method(x):
    print(x)

  @classmethod
  def c_method(cls,x):
    print(cls,x)

obj = Methods()

obj.i_method(1)
Methods.i_method(obj, 2)

obj.s_method(3)
Methods.s_method(4)

obj.c_method(5)
Methods.c_method(6)


Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization

YouTubeMy YouTube channel

Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong







Python tutorial



Python Home

Introduction

Running Python Programs (os, sys, import)

Modules and IDLE (Import, Reload, exec)

Object Types - Numbers, Strings, and None

Strings - Escape Sequence, Raw String, and Slicing

Strings - Methods

Formatting Strings - expressions and method calls

Files and os.path

Traversing directories recursively

Subprocess Module

Regular Expressions with Python

Regular Expressions Cheat Sheet

Object Types - Lists

Object Types - Dictionaries and Tuples

Functions def, *args, **kargs

Functions lambda

Built-in Functions

map, filter, and reduce

Decorators

List Comprehension

Sets (union/intersection) and itertools - Jaccard coefficient and shingling to check plagiarism

Hashing (Hash tables and hashlib)

Dictionary Comprehension with zip

The yield keyword

Generator Functions and Expressions

generator.send() method

Iterators

Classes and Instances (__init__, __call__, etc.)

if__name__ == '__main__'

argparse

Exceptions

@static method vs class method

Private attributes and private methods

bits, bytes, bitstring, and constBitStream

json.dump(s) and json.load(s)

Python Object Serialization - pickle and json

Python Object Serialization - yaml and json

Priority queue and heap queue data structure

Graph data structure

Dijkstra's shortest path algorithm

Prim's spanning tree algorithm

Closure

Functional programming in Python

Remote running a local file using ssh

SQLite 3 - A. Connecting to DB, create/drop table, and insert data into a table

SQLite 3 - B. Selecting, updating and deleting data

MongoDB with PyMongo I - Installing MongoDB ...

Python HTTP Web Services - urllib, httplib2

Web scraping with Selenium for checking domain availability

REST API : Http Requests for Humans with Flask

Blog app with Tornado

Multithreading ...

Python Network Programming I - Basic Server / Client : A Basics

Python Network Programming I - Basic Server / Client : B File Transfer

Python Network Programming II - Chat Server / Client

Python Network Programming III - Echo Server using socketserver network framework

Python Network Programming IV - Asynchronous Request Handling : ThreadingMixIn and ForkingMixIn

Python Coding Questions I

Python Coding Questions II

Python Coding Questions III

Python Coding Questions IV

Python Coding Questions V

Python Coding Questions VI

Python Coding Questions VII

Python Coding Questions VIII

Python Coding Questions IX

Python Coding Questions X

Image processing with Python image library Pillow

Python and C++ with SIP

PyDev with Eclipse

Matplotlib

Redis with Python

NumPy array basics A

NumPy Matrix and Linear Algebra

Pandas with NumPy and Matplotlib

Celluar Automata

Batch gradient descent algorithm

Longest Common Substring Algorithm

Python Unit Test - TDD using unittest.TestCase class

Simple tool - Google page ranking by keywords

Google App Hello World

Google App webapp2 and WSGI

Uploading Google App Hello World

Python 2 vs Python 3

virtualenv and virtualenvwrapper

Uploading a big file to AWS S3 using boto module

Scheduled stopping and starting an AWS instance

Cloudera CDH5 - Scheduled stopping and starting services

Removing Cloud Files - Rackspace API with curl and subprocess

Checking if a process is running/hanging and stop/run a scheduled task on Windows

Apache Spark 1.3 with PySpark (Spark Python API) Shell

Apache Spark 1.2 Streaming

bottle 0.12.7 - Fast and simple WSGI-micro framework for small web-applications ...

Flask app with Apache WSGI on Ubuntu14/CentOS7 ...

Selenium WebDriver

Fabric - streamlining the use of SSH for application deployment

Ansible Quick Preview - Setting up web servers with Nginx, configure enviroments, and deploy an App

Neural Networks with backpropagation for XOR using one hidden layer

NLP - NLTK (Natural Language Toolkit) ...

RabbitMQ(Message broker server) and Celery(Task queue) ...

OpenCV3 and Matplotlib ...

Simple tool - Concatenating slides using FFmpeg ...

iPython - Signal Processing with NumPy

iPython and Jupyter - Install Jupyter, iPython Notebook, drawing with Matplotlib, and publishing it to Github

iPython and Jupyter Notebook with Embedded D3.js

Downloading YouTube videos using youtube-dl embedded with Python

Machine Learning : scikit-learn ...

Django 1.6/1.8 Web Framework ...


Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong






OpenCV 3 image and video processing with Python



OpenCV 3 with Python

Image - OpenCV BGR : Matplotlib RGB

Basic image operations - pixel access

iPython - Signal Processing with NumPy

Signal Processing with NumPy I - FFT and DFT for sine, square waves, unitpulse, and random signal

Signal Processing with NumPy II - Image Fourier Transform : FFT & DFT

Inverse Fourier Transform of an Image with low pass filter: cv2.idft()

Image Histogram

Video Capture and Switching colorspaces - RGB / HSV

Adaptive Thresholding - Otsu's clustering-based image thresholding

Edge Detection - Sobel and Laplacian Kernels

Canny Edge Detection

Hough Transform - Circles

Watershed Algorithm : Marker-based Segmentation I

Watershed Algorithm : Marker-based Segmentation II

Image noise reduction : Non-local Means denoising algorithm

Image object detection : Face detection using Haar Cascade Classifiers

Image segmentation - Foreground extraction Grabcut algorithm based on graph cuts

Image Reconstruction - Inpainting (Interpolation) - Fast Marching Methods

Video : Mean shift object tracking

Machine Learning : Clustering - K-Means clustering I

Machine Learning : Clustering - K-Means clustering II

Machine Learning : Classification - k-nearest neighbors (k-NN) algorithm




Machine Learning with scikit-learn



scikit-learn installation

scikit-learn : Features and feature extraction - iris dataset

scikit-learn : Machine Learning Quick Preview

scikit-learn : Data Preprocessing I - Missing / Categorical data

scikit-learn : Data Preprocessing II - Partitioning a dataset / Feature scaling / Feature Selection / Regularization

scikit-learn : Data Preprocessing III - Dimensionality reduction vis Sequential feature selection / Assessing feature importance via random forests

Data Compression via Dimensionality Reduction I - Principal component analysis (PCA)

scikit-learn : Data Compression via Dimensionality Reduction II - Linear Discriminant Analysis (LDA)

scikit-learn : Data Compression via Dimensionality Reduction III - Nonlinear mappings via kernel principal component (KPCA) analysis

scikit-learn : Logistic Regression, Overfitting & regularization

scikit-learn : Supervised Learning & Unsupervised Learning - e.g. Unsupervised PCA dimensionality reduction with iris dataset

scikit-learn : Unsupervised_Learning - KMeans clustering with iris dataset

scikit-learn : Linearly Separable Data - Linear Model & (Gaussian) radial basis function kernel (RBF kernel)

scikit-learn : Decision Tree Learning I - Entropy, Gini, and Information Gain

scikit-learn : Decision Tree Learning II - Constructing the Decision Tree

scikit-learn : Random Decision Forests Classification

scikit-learn : Support Vector Machines (SVM)

scikit-learn : Support Vector Machines (SVM) II

Flask with Embedded Machine Learning I : Serializing with pickle and DB setup

Flask with Embedded Machine Learning II : Basic Flask App

Flask with Embedded Machine Learning III : Embedding Classifier

Flask with Embedded Machine Learning IV : Deploy

Flask with Embedded Machine Learning V : Updating the classifier

scikit-learn : Sample of a spam comment filter using SVM - classifying a good one or a bad one




Machine learning algorithms and concepts

Batch gradient descent algorithm

Single Layer Neural Network - Perceptron model on the Iris dataset using Heaviside step activation function

Batch gradient descent versus stochastic gradient descent

Single Layer Neural Network - Adaptive Linear Neuron using linear (identity) activation function with batch gradient descent method

Single Layer Neural Network : Adaptive Linear Neuron using linear (identity) activation function with stochastic gradient descent (SGD)

Logistic Regression

VC (Vapnik-Chervonenkis) Dimension and Shatter

Bias-variance tradeoff

Maximum Likelihood Estimation (MLE)

Neural Networks with backpropagation for XOR using one hidden layer

minHash

tf-idf weight

Natural Language Processing (NLP): Sentiment Analysis I (IMDb & bag-of-words)

Natural Language Processing (NLP): Sentiment Analysis II (tokenization, stemming, and stop words)

Natural Language Processing (NLP): Sentiment Analysis III (training & cross validation)

Natural Language Processing (NLP): Sentiment Analysis IV (out-of-core)

Locality-Sensitive Hashing (LSH) using Cosine Distance (Cosine Similarity)




Artificial Neural Networks (ANN)

[Note] Sources are available at Github - Jupyter notebook files

1. Introduction

2. Forward Propagation

3. Gradient Descent

4. Backpropagation of Errors

5. Checking gradient

6. Training via BFGS

7. Overfitting & Regularization

8. Deep Learning I : Image Recognition (Image uploading)

9. Deep Learning II : Image Recognition (Image classification)

10 - Deep Learning III : Deep Learning III : Theano, TensorFlow, and Keras









Contact

BogoToBogo
contactus@bogotobogo.com

Follow Bogotobogo

About Us

contactus@bogotobogo.com

YouTubeMy YouTube channel
Pacific Ave, San Francisco, CA 94115

Pacific Ave, San Francisco, CA 94115

Copyright © 2024, bogotobogo
Design: Web Master