| 1. |
What are class members? How do you define it? |
|
Answer» In Python, a class is defined by using the keyword class. Every class has a unique name followed by a colon ( : ). Syntax: class class_name: statement_1 statement_2 ………………… ………………… statement_n 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 = 10, 20 # class variables In the above code, name of the class is Sample and it has two variables x and y having the initial value 10 and 20 respectively. To access the values defined inside the class, you need an object or instance of the class. |
|