Skip to content

Commit dfaae58

Browse files
authored
Merge branch 'main' into apply-eslint-format
2 parents 55ceb59 + 505cd02 commit dfaae58

File tree

2 files changed

+196
-12
lines changed

2 files changed

+196
-12
lines changed

docs/python/python-constructor.md

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
---
2+
id: python-constructor
3+
title: Python Constructor
4+
sidebar_label: Python Constructor #displays in sidebar
5+
description: Learn about constructors in Python OOP, including the __init__ method, types of constructors, and real-world use cases.
6+
sidebar_position: 19
7+
tags:
8+
[
9+
Python,
10+
List in Python,
11+
Introduction of python,
12+
Python Syntax,
13+
Variables,
14+
Operators,
15+
Type Casting,
16+
String,
17+
Tuple in Python
18+
Array in Python
19+
Functions in Python
20+
Recursion in Python
21+
Opps in Python
22+
]
23+
---
24+
25+
# Constructor in Python
26+
27+
In Python, a **constructor** is a special method used to initialize the newly created object of a class. It is called automatically when a new object is created.
28+
29+
The most commonly used constructor in Python is the `__init__()` method.
30+
31+
---
32+
33+
## What is a Constructor?
34+
35+
A **constructor** is a special method in a class that is automatically called when an object is instantiated. It allows you to define and initialize the attributes of the object.
36+
37+
```python
38+
class Person:
39+
def __init__(self, name, age):
40+
self.name = name
41+
self.age = age
42+
43+
# Creating an object
44+
p1 = Person("Alice", 25)
45+
46+
print(p1.name) # Output: Alice
47+
print(p1.age) # Output: 25
48+
````
49+
50+
In the above example, `__init__()` is the constructor. It takes `name` and `age` as parameters and assigns them to the object's attributes.
51+
52+
---
53+
54+
## Syntax of `__init__()` Constructor
55+
56+
```python
57+
def __init__(self, parameters):
58+
# initialization code
59+
```
60+
61+
* `self` refers to the current instance of the class.
62+
* You can pass additional parameters to set initial values for the object.
63+
64+
---
65+
66+
## Types of Constructors in Python
67+
68+
### 1. Default Constructor
69+
70+
A constructor that takes only the `self` argument.
71+
72+
```python
73+
class Demo:
74+
def __init__(self):
75+
print("This is a default constructor")
76+
77+
obj = Demo()
78+
```
79+
80+
### 2. Parameterized Constructor
81+
82+
A constructor that takes additional arguments to initialize the object.
83+
84+
```python
85+
class Student:
86+
def __init__(self, name, grade):
87+
self.name = name
88+
self.grade = grade
89+
90+
s1 = Student("Ravi", "A")
91+
print(s1.name) # Output: Ravi
92+
print(s1.grade) # Output: A
93+
```
94+
95+
---
96+
97+
## Constructor with Default Values
98+
99+
You can also define default values for constructor parameters.
100+
101+
```python
102+
class Car:
103+
def __init__(self, brand="Tesla"):
104+
self.brand = brand
105+
106+
car1 = Car()
107+
car2 = Car("BMW")
108+
109+
print(car1.brand) # Output: Tesla
110+
print(car2.brand) # Output: BMW
111+
```
112+
113+
---
114+
115+
## Constructor in Inheritance
116+
117+
When using inheritance, the constructor of the base class can be called using `super()`.
118+
119+
```python
120+
class Animal:
121+
def __init__(self, species):
122+
self.species = species
123+
124+
class Dog(Animal):
125+
def __init__(self, species, name):
126+
super().__init__(species)
127+
self.name = name
128+
129+
d = Dog("Mammal", "Buddy")
130+
print(d.species) # Output: Mammal
131+
print(d.name) # Output: Buddy
132+
```
133+
134+
---
135+
136+
## Real-World Use Case: Managing a Library System
137+
138+
### Use Case: Library Book Management
139+
140+
Suppose you're building a **Library Management System** where each book has the following data: title, author, and availability status.
141+
142+
A constructor helps **initialize** the book’s data automatically when a book object is created.
143+
144+
```python
145+
class Book:
146+
def __init__(self, title, author, available=True):
147+
self.title = title
148+
self.author = author
149+
self.available = available
150+
151+
def display_info(self):
152+
status = "Available" if self.available else "Checked Out"
153+
print(f"{self.title} by {self.author} - {status}")
154+
155+
# Creating books
156+
book1 = Book("1984", "George Orwell")
157+
book2 = Book("The Alchemist", "Paulo Coelho", available=False)
158+
159+
book1.display_info() # Output: 1984 by George Orwell - Available
160+
book2.display_info() # Output: The Alchemist by Paulo Coelho - Checked Out
161+
```
162+
163+
### Why Constructor is Important Here?
164+
165+
* Ensures every book created has all the necessary data.
166+
* Automatically sets a default availability status (e.g., available = True).
167+
* Prevents manual initialization after creating the object.
168+
* Keeps the code clean, consistent, and modular.
169+
170+
Without a constructor, you'd have to write multiple lines of code every time a book is created, which can lead to errors and duplication.
171+
172+
---
173+
174+
## Summary
175+
176+
* Constructors are used to initialize object properties at the time of creation.
177+
* Python uses the `__init__()` method as a constructor.
178+
* Constructors can be default, parameterized, or inherited.
179+
* They improve code organization and reduce repetition.
180+
* Real-world use cases like Library Systems, Inventory Management, User Registration, etc., rely heavily on constructors for clean initialization.

src/pages/contact-us/index.css

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
--contact-shadow-hover: rgba(0, 0, 0, 0.3);
2424
--contact-input-bg: rgba(255, 255, 255, 0.05);
2525
--contact-input-border: rgba(255, 255, 255, 0.1);
26+
--select-text-primary: #000000;
2627
}
2728

2829
/* Light Theme Variables */
@@ -401,7 +402,7 @@ html[data-theme="light"] {
401402
.form-textarea {
402403
width: 100%;
403404
padding: 0.875rem 1rem;
404-
background: var(--contact-input-bg);
405+
background: var(--contact-input-bg); /* Already defined, but ensure it's applied */
405406
border: 1px solid var(--contact-input-border);
406407
border-radius: 0.75rem;
407408
color: var(--contact-text-primary);
@@ -411,13 +412,11 @@ html[data-theme="light"] {
411412
backdrop-filter: blur(5px);
412413
}
413414

414-
.form-input:focus,
415-
.form-select:focus,
416-
.form-textarea:focus {
417-
outline: none;
418-
border-color: var(--contact-accent-primary);
419-
background: var(--contact-bg-card-hover);
420-
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.2);
415+
/* Fix dropdown options visibility in dark mode */
416+
.form-select option {
417+
background: var(--contact-bg-card); /* Matches card background */
418+
color: var(--select-text-primary); /* Matches primary text color */
419+
padding: 0.5rem; /* Optional: Adds padding for better readability */
421420
}
422421

423422
.form-input::placeholder,
@@ -435,7 +434,7 @@ html[data-theme="light"] {
435434
appearance: none;
436435
background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3e%3cpath stroke='%236b7280' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='M6 8l4 4 4-4'/%3e%3c/svg%3e");
437436
background-position: right 0.75rem center;
438-
background-repeat: no-repeat;
437+
background-repeat: no-repeat; /* Ensure it doesn't repeat */
439438
background-size: 1.25rem;
440439
padding-right: 2.5rem;
441440
}
@@ -444,9 +443,14 @@ html[data-theme="light"] {
444443
background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3e%3cpath stroke='%23374151' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='M6 8l4 4 4-4'/%3e%3c/svg%3e");
445444
}
446445

447-
.form-select option {
448-
background: var(--contact-bg-card);
449-
color: var(--contact-text-primary);
446+
.form-select:focus {
447+
outline: none;
448+
border-color: var(--contact-accent-primary);
449+
background: var(--contact-bg-card-hover); /* Consistent background on focus */
450+
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.2);
451+
background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3e%3cpath stroke='%236b7280' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='M6 8l4 4 4-4'/%3e%3c/svg%3e"); /* Single arrow on focus */
452+
background-position: right 0.75rem center;
453+
background-repeat: no-repeat; /* Prevent repetition */
450454
}
451455

452456
.submit-button {

0 commit comments

Comments
 (0)