#๐Ÿ”’ import

315 messages ยท Page 1 of 1 (latest)

quiet apexBOT
#

@sullen imp

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

delicate field
#

hey

sullen imp
#

hey

delicate field
#

should we start from the previous question first?

sullen imp
#

yes

delicate field
#
if __name__ == '__main__':
    main()
sullen imp
#

it's called post

#

so u double __

delicate field
#

double underscore

sullen imp
#

oh

delicate field
#

its easy to write, hard to say

#

we often just say dunder

sullen imp
#

if name == 'gay':
main()

delicate field
#

lets unpack a few things first

#

i asked if you knew how imports in python work in detail

sullen imp
#

no

#

ik only,

import (package name)

delicate field
#

so lets look at that first, because it will be important to explain the double underscore if check above

#
import mypackage
#

here you have this import in your own file.py

sullen imp
#

i wanna be intermediate

delicate field
#

and you have some other code in mypackage.py

#

what do you think happends when you write import mypacakge ?

sullen imp
#

because you called file name

delicate field
#

when you import a file, all of the code inside that file will run

sullen imp
#

calculator =
print(eval(input())

delicate field
#

is that part clear?

sullen imp
#

and then to calculate u do

sudo fr -rm- get out

#

yes

#

run all code if called

delicate field
#

dont you remember what i said when you first asked?

sullen imp
#

i just answered yes br

#

wht wrong

delicate field
sullen imp
#

i heard eval can run any code

delicate field
sullen imp
#

ok

#

no eval

delicate field
#

you have too calm down and dont jump around

#

this will not be a quick explaination

sullen imp
#

just explain, not waste time on this

delicate field
#

sudo fr ..... and eval .. and other things while we are talking

#

you are the one wasting time

#

so, what was the last thing i talked about, and what did you understand from it?

sullen imp
#

bro, just explain

#

yes

#

if u do import filename then u run code from that file

delicate field
#

correct.. the key part is all the lines will run

sullen imp
#

yes

delicate field
#

keep that in mind, that will be important in a short while

#

a module, or python file, knoows its own name

#

so when you do import mypackage you can check its name

#
import mypackage
print(mypackage.__name__)
sullen imp
#

what

#

lemme test

delicate field
#

yes test it with a module that python has math

sullen imp
#

wait why u need name

#

if u alr called it

delicate field
#

!e

import math
print(math.__name__)
sullen imp
#

with name

quiet apexBOT
delicate field
sullen imp
#

ooh

delicate field
#

lets say you have some code that should only work if you import the math module

sullen imp
#

how to do that

delicate field
#

you can check that you have imported the math module by its name

delicate field
#
import math

if math.__name__ == 'math':
    print('yay you have imported math')
#

we can do this, but we often dont do this in this way, we do the inverse

sullen imp
#

else: ?

#

inverse?

delicate field
delicate field
sullen imp
#

oh

delicate field
#

instead of checking if we have imported math, we check if we are running math

#

the oposite of importing a file is to run the file

#
py file.py
#

this is how you would run a python file

sullen imp
#

in vscode

delicate field
#

you can also run it by clicking your green play button in vscode

sullen imp
#

yes

#

I don't use it anymore

delicate field
#

the play button just runs py file.py in the background

#

python is made to know the difference between a file you have run and a file you have imported

#

and it know the difference between the two based on the module name

#

the file you run, will be renamed

sullen imp
#

import random

if random.name == 'random':
print('yay') ?

delicate field
#

it will be renamed to __main__

sullen imp
#

then i can call my code that uses random

delicate field
#

well, i said you do the inverse of this

sullen imp
#

and at end do else that will just ignore if not imported

delicate field
#

you dont really check if an imported name is equal to itself,

#

you check the oposite, ytou check if the file has been run or if the file has been imported

#

and i said, the file you run, will be renamed

#

!e

print(__name__)
quiet apexBOT
sullen imp
#

if you check if file run or imported and it will rename file

delicate field
#

you can test it yourself

sullen imp
#

hmm

delicate field
#

make a.py and b.py and have print(__name__) in both

sullen imp
#

if i do this i'll rename file to main?

delicate field
#

run them each at a time

delicate field
#

and the name will be __main__

sullen imp
#

ooh

#

i need code example of use in real code

#

please

delicate field
#
NAME = 'eivl'
AGE = '43'

def greet(name, age):
    print(f'Hello {name}, you are {age} years old')


greet(NAME, AGE)
#

put this code in greet.py

#

you need two files for this greet.py and main.py

#

let me know whhen you have done these two things

sullen imp
#

you 43?

delicate field
#

yeah

#

been writing python for a while! ๐Ÿ˜„

sullen imp
#

wow

#

wait

#

so

#

like how

#

wait

#

so i just import it

delicate field
sullen imp
#

ok

delicate field
#

your main.py file is empty.. lets add some code to it

#

your greet.py file has the code above right?

sullen imp
#

not yet

#

i installing ide

#

done

delicate field
#

let me know when you have a greet.py file with the content above, and an empty main.py file

sullen imp
delicate field
#

age is picked randomly arbitary

#

๐Ÿ˜„

#

i want you to run main.py and see what happends

#

show me what happends i mean

sullen imp
#

Unexpected identifier 'main'
SyntaxError
24.10.2025, 09:50:22

#

so i have to do that as function

delicate field
#

show what you have done please

sullen imp
#

pasted code u said

delicate field
#

share a screenshot of what you have done please

sullen imp
delicate field
#

i dont know what editor this is though

#

the files needs to be saved and be in the same locatioon

sullen imp
#

acode

#

i just pasted same code

#

it looks absolutely same

#

just one space at line 1

#

because of ```py

delicate field
delicate field
abstract karma
#

(but a blank line is fine)

delicate field
#

yes, the issue is you have an space at the start

sullen imp
#

ok

delicate field
#

i can run it on my machine and show you if you like to see the example i wrote

#

notice a thing here @sullen imp

#

when i run main.py prints out my greet and your greet

sullen imp
#

ok

#

why connected to a git branch

delicate field
#

that is because, when you import something, all lines of the file is run

#

but we dont want that

#

we want greet.py to behave one way when i run it, and another way when i import it

#

when i import the code, i dont want to run the greet() function

#

withh me on this?

abstract karma
sullen imp
#

still same error

#

what u even importing

delicate field
sullen imp
#

there no function called greet

delicate field
#

yes there is. a named my function greet

sullen imp
#

you send me code without that bro

delicate field
sullen imp
#

you did

#

oh wait yeah

#

you didn't

#

hm

#

what about call it at top

delicate field
delicate field
#

can i show you the rest?

sullen imp
#

bruh

#

didn't worked

#

lemme try code all myself

#

brb

delicate field
abstract karma
delicate field
#

we already talked about this

#

and you said i could explain, but your not letting me do that

#

you can run code here on discord

#

!e

NAME = 'eivl'
AGE = '43'

def greet(name, age):
    print(f'Hello {name}, you are {age} years old')


greet(NAME, AGE)
quiet apexBOT
sullen imp
#

bruh, ig my IDE just sucks

delicate field
sullen imp
#

no

#

so

#

it just imports function

#

if just import and file name

#

runs all lines

delicate field
#

and what happends when you import something?

delicate field
sullen imp
#

if from filename import functon then just function

#

if you defined it

#

and same folder

delicate field
#
from greet import greet

greet(name='sorncss', age=16)

how many times are greet function run when you do these?

sullen imp
#

So that's all about imports?

delicate field
delicate field
sullen imp
#

yes

delicate field
#

now i will add one more line to fix this issue of it priting two times

sullen imp
#

because u call function which u called previously and define own age and name

delicate field
#

it will fix the issue of code running all lines when importing

delicate field
#

i want to guard agains that code getting run when i import

#

but still import it when i run the file

sullen imp
#

how??

delicate field
#

you check if the __name__ is __main__

sullen imp
#

ohhh

delicate field
#

now i can from greet import greet without that extra greet function beeing run

delicate field
sullen imp
#

soo it run only if u do main, not greet and main same time

#

it calls it only in main

delicate field
#

remember what i said earlier, python renames __name__ to __main__ for the python file you run

sullen imp
#

ok

#

I understand

#

another question

#

could i ask

delicate field
#

fantastic!

#

ask away

sullen imp
#

what is class

#

ik case

#

but class

delicate field
#

class is a way to write code in a special way called object oriented

#

it is something you will learn more about in the future

sullen imp
#

match name:
case "John"
print('lol')
case _:
print('who?')

delicate field
#

just be carefull of where you learn about it. people have a lot of strange oppininons about how to teach classes to people

sullen imp
#

sm like that

#

don't remember

delicate field
#

do you wonder about match case ?

sullen imp
#

ik

#

ik it

#

i watched about it in cs50

#

gimme example code soi save somewhere

#

not sure if my correct

delicate field
#

pattern matching is something you should practise and learn, it is very useful.. you cant learn it from watching, so you have to do the work yourself

sullen imp
#

match name:
case ('John')
print("hello, john")
case _:
print("Who?")

#

this?

#

this video is a fun take on interpreted vs compiled language
follow form more programming content specially in javascript , python , c++ , java and become skillful coder and developer

Don't forget to LIKE, COMMENT, and SUBSCRIBE for more tech comedy and insightful developer content!

#programming #coding #DeveloperLife#WebDevelopment #UIUXDesig...

โ–ถ Play video
delicate field
#

i would not worry about pattern matching until you have learned the fundamentals of python

sullen imp
#

what it used for

#

could i have example

delicate field
#

i can explain what it is used for if you want, but i will not give out examples. those examples will confuse you. you have to have a solid foundation first

sullen imp
#

ok

delicate field
#

have you use lists, tuples and dicts before?

sullen imp
#

never

#

I don't like gpt, it's kinda dumb

delicate field
#

do you know what they are?

sullen imp
#

stack overflow better

#

no

delicate field
#

then im not able to explain pattern matching clearly

#

you should focus on learning the basics and fundamentals of python

#

the book you where recommended multiple times, it goes over all these

sullen imp
#

no books

#

too long to read

delicate field
#

then you will not learn programming

sullen imp
#

ok

delicate field
#

you have to read and write

#

watching someone else do the work will not give you the option to learn it

#

at best, you will only recognise it

#

and you cant learn programming quickly

sullen imp
#

@delicate field

delicate field
#

thats why those 15 hours videos are silly

delicate field
sullen imp
#

look

#

i this bro can code

#

me watch it rq

delicate field
#

there is no way for me to explain advanced concepts to you if you have not spend the required time to learn about the fundamentals

#

you cant learn thiis from watching a video

sullen imp
#

bro

delicate field
#

so take the recommendation you have been given and do the work

sullen imp
#

i need to read a book in big 2025

delicate field
#

if it feels hard, your doing it right

#

if you feel its easy, your doing it wrong

sullen imp
#

when i can watch hundreds of guides, and try code myself

#

so like lists is justvaluee, strings inside a [

delicate field
sullen imp
#

like

#

fruits = [melon, kiwi]

#

lemme watch more of this vid

delicate field
#

you should stay away from youtube to learn programming,

#

you should do what we have recommended you to do.

#

but we are not here to force you, only give out advice and help! ๐Ÿ˜„

sullen imp
#

i will not read books

delicate field
#

then my previous comment on this stand firm

#

did you have any other questions?

sullen imp
#

no

delicate field
#

then have a great day and good luck! ๐Ÿ˜„

sullen imp
#

is dictionaries ril hard

#

so this dude needed to do another vid for it

quiet apexBOT
#

:warning: The owner of this post is no longer in the server.

delicate field
#

!close

quiet apexBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.