📖 generic · 12th TN - English Medium · COMPUTER SCIENCE · Page 176question

PYTHON CLASSES AND OBJECTS

Chapter 3: 4 · COMPUTER SCIENCE

PYTHON CLASSES AND OBJECTS 12th Computer Chapter - - Python Classes and Objects Where, statement in a class definition may be a variable declaration, decision control, loop or even a function definition. Variables defined inside a class are called as “Class Variable” and functions are called as “Methods”. Class variable and methods are together known as members of the class. The class members should be accessed through objects or instance of class.

A class can be defined anywhere in a Python program. Example: Program to define a class class Sample: x, y = , # class variables In the above code, name of the class is Sample and it has two variables x and y having the initial value and respectively. To access the values defined inside the class, you need an object or instance of the class. Creating Objects .

Once a class is created, next you should create an object or instance of that class. The process of creating object is called as “Class Instantiation”. Syntax: = ( ) Note that the class instantiation uses function notation ie. with () Accessing Class Members .

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

class Sample: x, y = , #class variables 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 = Value of y = Value of x and y = Example : Program to define a class and access its member variables 12th Computer Chapter - - 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 and 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

Related topics

Have a question about this topic?

Get an AI answer grounded in your actual textbook — with the exact page reference.

Ask AI about this topic →