- def keyword in Python is used for defining a function. It is synonymous with the word definition.
- In case of classes the def keyword is used for defining the methods of a class.
- Note that the keyword self needs to be passed as the first parameter in member function definitions.
- def keyword is also required to define special member function of a class like __init__().
In this example Python program a function is defined using the def keyword.
radiusValue = 2
circleArea = areaOfTheCircle(radiusValue)
print("Area of the circle with %f units radius is %f units"%(radiusValue, circleArea));
Area of the circle with 2.000000 units radius is 12.566371 units
In this example Python program a method of a class is defined using the def keyword.
myRadius = 0
def __init__(self, radius):
self.myRadius = radius
radius = 5
circle = Circle(radius)
print("Area of the circle with %f units radius is %f units"%(radius, circle.area()));
Area of the circle with 5.000000 units radius is 78.539816 units