Sunday, 25 October 2026

Python Coding challenge - Day 1290| What is the output of the following Python Code?

 


Code Explanation:

1. Defining the Class

class Test:

- The class keyword is used to define a class named Test.

- A class acts as a blueprint for creating objects.

- Here, the class contains a class variable and a static method.


2. Creating a Class Variable

x = 10

- The variable x is defined inside the class but outside any method.

- It is called a class variable because it belongs to the class.

- Its initial value is 10.

- It can be accessed using Test.x.


3. Using the @staticmethod Decorator

@staticmethod

- This decorator defines the method below it as a static method.

- A static method does not automatically receive self or cls as its first argument.

- It can be called through the class or an instance.


4. Defining the Static Method

def show(): return Test.x

- The show() method returns the value of Test.x.

- It directly accesses the class variable through the class name Test.

- Because it uses Test.x, it reads the class variable rather than an instance-specific attribute.


5. Creating an Object

obj = Test()

- Test() creates an instance of the Test class.

- The created object is assigned to the variable obj.

- Initially, obj can access the class variable x, whose value is 10.


6. Creating an Instance Attribute

obj.x = 20

- This statement creates an instance attribute named x on obj.

- Its value is set to 20.

- It does not change the class variable Test.x, which remains 10.

- The instance attribute shadows the class variable when accessing obj.x.


7. Calling the Method and Printing Values

print(obj.show(), Test.x)

- obj.show() calls the static method.

- The method returns Test.x, which is 10.

- Test.x also evaluates to 10 because the class variable was not modified.

- The instance attribute obj.x = 20 does not affect the method, because the method explicitly accesses Test.x.


Final Output

10 10

Books: 100 Python Programs for Beginner with explanation

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (354) Android (25) AngularJS (1) API (2) Assembly Language (2) aws (31) Azure (12) BI (10) book (1) Books (359) Bootcamp (16) C (78) C# (12) C++ (83) cloud (1) Course (96) Coursera (306) Cybersecurity (37) data (10) Data Analysis (47) Data Analytics (31) data management (16) Data Science (440) Data Strucures (21) Deep Learning (224) Django (17) Downloads (3) edx (21) Engineering (15) Euron (30) Events (9) Excel (24) Finance (13) flask (4) flutter (1) FPL (17) Gadgets (1) Generative AI (79) Git (14) Google (55) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (413) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (16) PHP (20) Projects (35) Python (1395) Python Coding Challenge (1287) Python Library (28) Python Mathematics (22) Python Mistakes (51) Python Pattern Challenge (32) python quixz (1) Python Quiz (669) Python Tips (114) Questions (3) R (72) React (7) Scripting (3) security (5) Selenium Webdriver (4) Software (21) SQL (56) Udemy (24) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)