לימוד פייתון : מחלקות 2

The Python __main__ entry point

Or more formally:

The Python “main guard”

This Is Important

It allows one file to have two behaviors:

  1. Behave like a program
    Run main() when executed from terminal.

  2. Behave like a library
    Provide classes, functions, etc. when imported.

Example:

python vectors.py

Output will run main().

But:

import vectors

Will NOT run main — only the classes become available.

main() function to show proper program structure.
Clear, simple, copy-paste ready for WordPress.

This example teaches:

  • How to define a class

  • How to use self

  • How to write a clean main()

  • How to run class logic only when the file is executed


FULL CODE SNIPPET (CLASS + MAIN)

############################################################
# CLASS EXAMPLE WITH MAIN()
# We create a Vector2D class and use it inside main()
############################################################

import math

class Vector2D:
def __init__(self, x, y):
# store values inside the object
self.x = x
self.y = y

def length(self):
# compute vector magnitude
return math.sqrt(self.x*self.x + self.y*self.y)

def add(self, other):
# add two vectors
return Vector2D(self.x + other.x, self.y + other.y)

def __repr__(self):
return f"Vector2D({self.x}, {self.y})"

############################################################
# MAIN FUNCTION
############################################################

def main():
print("=== Vector2D Demo ===")

v1 = Vector2D(3, 4)
v2 = Vector2D(1, 2)

print("v1 =", v1)
print("v2 =", v2)

print("Length of v1:", v1.length())
print("Length of v2:", v2.length())

v3 = v1.add(v2)
print("v1 + v2 =", v3)

print("=== END ===")

############################################################
# RUN MAIN ONLY IF FILE EXECUTED DIRECTLY
############################################################

if __name__ == "__main__":
main()


Explanation

1. Class definition

class Vector2D:

A 2D vector with x and y.

2. __init__ and self

def __init__(self, x, y):
self.x = x
self.y = y

self stores data inside the object.

3. Methods use self and other

def add(self, other):
return Vector2D(self.x + other.x, self.y + other.y)

self is the object before the dot.
other is the object passed into the method.

4. main() function

def main():
...

Python will run this like a normal program.

5. Program entry point

if __name__ == "__main__":
main()

Means “run main() only if this file is executed directly”.


If you want, I can now give:

Next Level Example:
A class with inheritance (Animal → Dog → Cat),
or an AI-oriented example (Matrix class, Layer class, Neuron class, etc.)

Tell me which direction you prefer.

You said:
if __name__ == "__main__": main() Means “run main() only if this file is executed directly”. not clear
ChatGPT said:

Here is the clearest possible explanation of:

if __name__ == "__main__":
main()

Short, accurate, and simple.


The Problem It Solves

Python files can be used in two different ways:

1. You run the file directly

Example:

python my_program.py

2. Another file imports it

Example:

import my_program

When someone imports your file, you do NOT want main() to run automatically.

You only want main() to run when the file itself is executed.


What Python Does Internally

Every Python file has a built-in variable called __name__.

Python sets it to:

Case 1 — File is RUN directly:

__name__ == "__main__"

Case 2 — File is IMPORTED:

__name__ == "the_file_name"

Example:
If your file is called math_utils.py and someone writes:

import math_utils

then inside that file:

__name__ == "math_utils"

So the condition fails.


The Logic

if __name__ == "__main__":
main()

Means:

If this file is run directly → run main()

If this file is imported → do NOT run main()