Home | | Computer Science 12th Std | Accessing Class Members

Python - Accessing Class Members | 12th Computer Science : Chapter 10 : Python Modularity and OOPS : Python Classes and Objects

Chapter: 12th Computer Science : Chapter 10 : Python Modularity and OOPS : Python Classes and Objects

Accessing Class Members

Any class member ie. class variable or method (function) can be accessed by using object with a dot ( . ) operator.

Accessing Class Members

Any class member ie. class variable or method (function) can be accessed by using object with a dot ( . ) operator.

Syntax:

Object_name . class_member

Example : Program to define a class and access its member variables

class Sample:

#class variables

x, y = 10, 20

S=Sample( )                              # class instantiation

print("Value of x = ", S.x)

print("Value of y = ", S.y)

print("Value of x and y = ", S.x+S.y)

Output :

Value of x = 10

Value of y = 20

Value of x and y = 30

In the above code, the name of the class is Sample. Inside the class, we have assigned the variables x and y with initial value 10 and 20 respectively. These two variables are called as class variables or member variables of the class. In class instantiation process, we have created an object S to access the members of the class. The first two print statements simply print the value of class variable x and y and the last print statement add the two values and print the result.

 

Tags : Python , 12th Computer Science : Chapter 10 : Python Modularity and OOPS : Python Classes and Objects
Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
12th Computer Science : Chapter 10 : Python Modularity and OOPS : Python Classes and Objects : Accessing Class Members | Python


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.