Creating class and Objects In Python Programming MSBTE
Creating classes and Objects In Python Programming MSBTE |
Creating Classes and Objects
Being an object-oriented programmer, we know that a class is a set of data members (attributes) and member-functions (operations). We also know that each real-world entity (or thing) can be referred to as an object, and an object is an instance of some class.
Creating Class
- To create a class in Python, general syntax is much more like other object-oriented languages.
class class_name:
- To create a class, keyword 'class' followed by a class name and a colon. The data-members and member-functions are defined under this signature with an indent. For example,
- class cwpediaTest1 :
- def helloCwpedia(self):
- print("Hello From cwpediaTest")
- def hiCwpedia(self):
- print("Hi from class cwpediaTest")
- Note the indents given in the above example. Two member functions helloCwpedia() and hiCwpedia() are defined within Indent of class cwpediaTest1. And the statements of both member functions are written within indent of specific member-functions. Similarly, to add data-members refer following class.
- class cwpediaTest2:
- a = 5
- b = 4
- def helloCwpedia(self):
- print("Hello From Class cwpediaTest2")
- def hiCwpedia(self):
- print("Hi From Class cwpediaTest2")
- Again note the indents for data-members and member functions. The data-members must be initialized. (Because python don’t allow explicit declaration of data-members).
- And the scope of data-members is up to that class. Unlike other languages, to use data-members in member-functions we must take the help of parameter ‘self’ which is almost like ‘this pointer’ of C++ and ‘this’ of Java. Refer following another class where data-members are accessed by using parameter 'self’.
- class cwpediaTest3:
- a = 5
- def get_a(self):
- print("Enter value of a: ")
- self.a = int(input())
- def show_a(self):
- print("a =",self.a)
- def reset_a(self):
- self.a=5
- Line-2 declares data-member ‘a’ and it was accessed in line-5, line-7, and line-9 by using parameter ‘self’ in three different member-functions. We can call these member functions in the required order by using an object from anywhere.
Declaring Object:-
- An object is an instance of a class. We can also say that an object allocates memory to data-members (attributes) of class and member-functions perform desired operations on the attributes of invoking objects. Following is the syntax to create an object of a class.
Object_name = class_name( )
- For example, to declare the object of the above class cwpediaTest3, then it will be as follows.
obj = Test 3()
- Don't forget the parenthesis( ( ) ) with class name cwpediaTest3. If parenthesis is not applied, then ‘obj’ won't be object, but instead, it will be a reference. And a reference will not be helpful to call/use/access members of class cwpediaTest3
- After declaring the object, we can work with data-members and member-functions by using object_name and dot operator ( . ). Refer following complete example.
- class cwpediaTest2:
- a = 5
- b = 4
- def helloCwpedia(self):
- print("Hello From Class cwpediaTest2")
- def hiCwpedia(self):
- print("Hi From Class cwpediaTest2")
- obj = cwpediaTest2()
- print("a =",obj.a)
- print("b =",obj.b)
- obj.helloCwpedia()
- obj.hiCwpedia()
- This will Show following output:-
a = 5
b = 4
Hello From Class cwpediaTest2
Hi From Class cwpediaTest2
b = 4
Hello From Class cwpediaTest2
Hi From Class cwpediaTest2
- Similarly, we can declare any number of objects of a class and work on its members. For example, the following statements will create three different objects of class cwpediaTest2.
obj1 = cwpediaTest2()
obj2 = cwpediaTest2()
obj3 = cwpediaTest2()
or in single line
obj1,obj2,obj3 = cwpediaTest2(),cwpediaTest2(),cwpediaTest2()
obj2 = cwpediaTest2()
obj3 = cwpediaTest2()
or in single line
obj1,obj2,obj3 = cwpediaTest2(),cwpediaTest2(),cwpediaTest2()
- All three objects obj1, obj2, and obj3 will have individual memory and hence, they will have an individual set of attributes a and b.
Post a Comment