How to create an object in python?

786    Asked by PavithraShah in Data Science , Asked on Jan 16, 2020
Answered by Pavithra Shah

We saw above that we can use a class object to access its own different attributes. It can also be used to create a new object and process is to create a new object is mentioned below:

ob = MyClass()

This code line will create an object instance “ob” for class “MyClass()” defined in the previous question. By using this new object you can access Myclass() attributes.

See below code for clear understanding:

class MyClass2:

 "This is my second class"

 a = 50

 def func(self):

  print('Janbask Training')

# create a new MyClass

ob = MyClass2()


print(MyClass2.func)

# Output:

print(ob.func)

# Output: >

# Calling function func()

ob.func()

# Output: Hello



Your Answer

Interviews

Parent Categories