Python Define Class


Define Class

How to define a class in Python language ?

First you need to know that a class is an object type created by executing a class statement.

The simple way to write a class is to use class keyword, then write de class name, and inside the class you can declare variables, functions, methods.
Methods are defined as functions inside the class definition. When declare an function or method inside the class, then use self keyword for the name of first argument.

Examples

Create simple class

>>> class TestClass:
	a = "Test"
	def f1(self):
		return print("The class output")

		
>>> x = TestClass()
>>> x.a
'Test'
>>> x.f1()
The class output
>>> 

Define class with more arguments

>>> class myClass:
	def sum(self, arg1, arg2, arg3):
		s = arg1 + arg2 + arg3
		return s

	
>>> x = myClass()
>>> x.sum(7,1,2)
10
>>>