Menu

Difference between function and method in Python

Let’s understand the difference between Python methods and functions and when and how to use function or method:

  1. Functions:

    • Functions are standalone blocks of code that can be called by their name.
    • They are defined independently and are not associated with any specific class or object.
    • Functions can have zero or more parameters.
    • They may or may not return any data.
    • Example of a user-defined function:
      def subtract(a, b):
          return a - b
      
      print(subtract(10, 12))  # Output: -2
      
  2. Methods:

    • Methods are special types of functions associated with a specific class or object.
    • They are defined within a class and are dependent on that class.
    • A method always includes a self parameter (for instance methods) or a cls parameter (for class methods).
    • Methods operate on the data (instance variables) contained by the corresponding class.
    • Example of a user-defined method:
      class ABC:
          def method_abc(self):
              print("I am in method_abc of ABC class.")
      
      class_ref = ABC()  # Create an object of ABC class
      class_ref.method_abc()  # Output: "I am in method_abc of ABC class." 

In final conclusion, functions are independent, while methods are associated with classes or objects. Functions can be called directly by their name, but methods require invoking the class or object they belong to. 

Java Interface examples

In this blog we will see different ways of implement interface in Java.

For example we have two interfaces, IntOne and IntTwo as below.

interface IntOne {
    public String getSrc();
}

interface IntTwo {
    public String getName();
}

1. Simple one and one implements example of interface

class IntOneImpl implements IntOne {
    private String src = "Source Interface One";
    
    public String getSrc() {
        return src;        
    } 
}

class IntTwoImpl implements IntTwo {
    private String name = "Java Interface Examples";
    
    public String getName(){
        return name;
    }
    
}

public class Main
{
public static void main(String[] args) {
IntTwoImpl in = new IntTwoImpl();
IntOneImpl one = new IntOneImpl();
System.out.println(one.getSrc());
System.out.println(in.getName());
}
}

This will work fine and give you output 
Source Interface One
Java Interface Examples

2. Lets see if we implement both the interfaces in a single class and access their methods.


class IntOneImpl implements IntOne {
    private String src = "Source Interface One";
    
    public String getSrc() {
        return src;        
    } 
}

class IntTwoImpl implements IntOne, IntTwo {
    private String name = "Java Interface Examples";
    IntOneImpl intOne = new IntOneImpl();
    @Override
    public String getSrc() {
        return intOne.getSrc();
    }
    
    @Override
    public String getName(){
        return name;
    }
    
}

public class Main
{
public static void main(String[] args) {
IntTwoImpl in = new IntTwoImpl();
System.out.println(in.getSrc());
System.out.println(in.getName());
}
}

Lets see what is happeing in this code.

a. IntOneImpl implements the IntOne and provide definition of getSrc method.

b. IntTwoImpl implements IntOne and IntTwo interfaces and override methods from both the interfaces. 

 


Frame.io version 4

With the new Frame.io version 4, a user can customize and manage any end-to-end creative workflow faster and more effectively than ever before. Adobe is launching this features very soon. to know more and try it free for a while please do visit frame.io

New features added in Frame.io are:

1. Share from one centralize place to anywhere.

2. Share with customized or branded look and feel.

3. Easy collaboration with teams, users, and projects for design and customization.

4. Bulk upload.

5. Rich interface.

6. Frame level review.