Differences between `class` and` def`

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

+7
python user-interface django
source share
2 answers

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

def used to define a function or method. The method is similar to a function belonging 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" 

Think about the Django part of your question, sorry. Perhaps this should be a separate issue?

+10
source share

class defines the class.

def defines a function.

 class Foo: def Bar(self): pass def Baz(): pass f = Foo() # Making an instance of a class. f.Bar() # Calling a method (function) of that class. Baz() # calling a free function 
+3
source share

All Articles