Change language

Object Oriented Programming in Python | Set 1 (class, object and members)

# Simple class example

class Test:

 

# Method example

def fun ( self ):

  print ( "Hello" )

 
# Driver code

obj = Test ()

obj.fun ()

 

Output:

 Hello 

As we can see above, we are creating a new class using the class operator and the class name. This is followed by an indented block that forms the body of the class. In this case, we have defined one method in the class.

Then we create an object / instance of that class using the class name and a couple of parentheses.

I

  1. Class methods must have an additional first parameter in the method definition. We do not give a value for this parameter when calling the method, Python provides it
  2. If we have a method that takes no arguments, then we still need to have one argument — self. See fun () in the simple example above.
  3. It’s like this pointer in C++ and this reference in Java.

When we call a method on this object as myobject .method (arg1, arg2), it is automatically converted by Python to MyClass.method (myobject, arg1, arg2) — that’s all about the special self.

The __init__ method
The __init__ method is similar to constructors in C++ and Java. It runs as soon as an object of the class is instantiated. This method is useful for doing whatever initialization you want to do on your object.

# Example class with init method

class Person:

 

# init method or constructor

  def __ init __ ( self , name):

  self .name = name

  

# Method example

< code class = "undefined spaces">  def say_hi ( self ):

print ( ’Hello, my name is’ , self . name)

  

p = Person ( ’Shwetanshu’ )

p.say_hi ()

Output:

 Hello, my name is Shwetanshu 

Here we define the __init__ method as a parameter name (along with the usual self). ,

Class and instance variables (or attributes)
In Python, instance variables — these are variables whose value is assigned within a constructor or method using self.

Class Variables — these are variables whose values ​​are assigned in the class.

# Python program to show that variables with a value
# assigned in the class declaration, are class variables and
# variables within methods and constructors are instances
# variables.

 
# Class for computer science students

class CSStudent:

 

  # Class variable

stream = ’cse’  < / p>

 

# init method or constructor

def __ init __ ( self , roll):

 

# Instance variable

self . roll = roll 

 
# CSStudent class objects

a = CSStudent ( 101 )

b = CSStudent ( 102 )

  

print (a.stream)  # prints & quot; cse & quot;

print (b.stream)  # prints & quot; cse & quot;

print (a.roll )  # prints 101

 
# Class variables can be accessed using the class
# name also

print (CSStudent.stream) # prints & quot; cse & quot;

We can also define instance variables inside regular methods.

# Python program to show what we can create
# instance variables inside methods

 
# Class for computer science students

class CSStudent:

 

# Class variable

stream = ’cse’  

  

# init method or constructor

def __ init __ (< / code> self , roll):

 

# Instance variable

self . roll = roll 

 

# Adds an instance variable

def setAddress ( self , address):

self . address = address

 

  # Get an instance variable

  def getAddress ( self ): 

return self .address 

 
Driver code

a = CSStudent ( 101 )

a.setAddress ( " Noida, UP " )

print ( a.getAddress ()) 

Output:

 Noida, UP 

How to create an empty class?
We we can create an empty class with the

# Empty class

class Test:

pass

Object Oriented Programming in Python | Set 2 (Hiding Data and Printing Objects)

This article is courtesy of Shwetanshu Rohatgi . Please post comments if you find something wrong or if you would like to share more information on the topic under discussion

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method