PYTHON - CLASS

             

                      


The simplest form of class definition looks like this:
class ClassName:
    <statement-1>
    .
    .
    .
    <statement-N>
Class definitions, like function definitions (def statements) must be executed before they have any effect. (You could conceivably place a class definition in a branch of an if statement, or inside a function.)
In practice, the statements inside a class definition will usually be function definitions, but other statements are allowed, and sometimes useful — we’ll come back to this later. The function definitions inside a class normally have a peculiar form of argument list, dictated by the calling conventions for methods — again, this is explained later.
When a class definition is entered, a new namespace is created, and used as the local scope — thus, all assignments to local variables go into this new namespace. In particular, function definitions bind the name of the new function here

  Python Class Example

class calcuator:  #define class
 @staticmethod
 def add(x,y):
add=x+y
print(add)
 @staticmethod
 def sub(x,y):
sub=x-y
print(sub)
 @staticmethod
 def div(x,y):
div=x/y
print(div)
 @staticmethod
 def mult(x,y):
mul=x*y
print(mul)
#call staticmethod add directly 
#without declaring instance and accessing class variables
calcuator.add(3,5)

calcuator.sub(3,5)

calcuator.mult(3,5)

calcuator.div(3,5)
############## output ###########################################################
8
-2
15
0.6


Comments