Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between `class` and `def` [closed]

What is the main difference between class and def in python? Can a class in python interact with django UI (buttons)?

like image 222
Slow learner Avatar asked Sep 18 '13 08:09

Slow learner


People also ask

What is the difference between class and Def?

class is used to define a class (a template from which you can instantiate objects). def is used to define a function or a method. A method is like a function that belongs to a class.

What's the difference between a class and a function?

Functions do specific things, classes are specific things. Classes often have methods, which are functions that are associated with a particular class, and do things associated with the thing that the class is - but if all you want is to do something, a function is all you need.

What is a class and DEF in Python?

Classes are like a blueprint or a prototype that you can define to use to create objects. We define classes by using the class keyword, similar to how we define functions by using the def keyword.

What is the difference between classes and functions in Javascript?

One key distinction between functions and classes was highlighted in this talk which suggests that a function is a behavior that can carry data while, inversely, a class is data that can carry behavior.


1 Answers

class is used to define a class (a template from which you can instantiate objects).

def is used to define a function or a method. A method is like a function that belongs to a class.

# function
def double(x):
    return x * 2

# class
class MyClass(object):
    # method
    def myMethod(self):
        print ("Hello, World")

myObject = MyClass()
myObject.myMethod()  # will print "Hello, World"

print(double(5))  # will print 10

No idea about the Django part of your question sorry. Perhaps it should be a separate question?

like image 81
nakedfanatic Avatar answered Nov 11 '22 16:11

nakedfanatic