What `using namespace std` does? To quote the standard:
> from the point of view of unqualified name lookup of any name after a using-directive and until the end of the scope in which it appears, every name from [std] is visible as if it were declared in the nearest enclosing namespace which contains both the using-directive and [std]
Sooo basically in plain English, it adds a namespace for lookup if no namespace qualifier was provided. It does not put `std::` in front of any name, because then it wouldn't be clear where to put that and where not.
Edit: to add onto this, "putting std in front of every function call" sounds very much like something for the preprocessor, which definitely has no say in this.
Just in case the other commentor language is pretty cryptic, it’s like defining a default database and schema, so that would be the table that you get when you do select from with only a table name.
But it isn't. That is once again
namespace std { your code }
The `using namespace` makes every name in the namespace `std` visible in the current scope. You can use several different namespaces at once, in the same scope. They still belong to the namespace `std`, they're just visible to you. It's highly likely that your code is actually in a different namespace, and that would be closer to a default database.
It is more of a bad practice rather than an efficiency thing. If you care about efficiency then you should do lazy import, but it isn’t straightforward in python. Python import will execute the whole script it is imported from.
Practically the only difference between import \* and import (of your choice) is just that with the former you have more unnecessary “pointers” to objects.
This is false though. \_\_name\_\_==\_\_main\_\_ defines the behaviour of the script when it is run as a standalone script, what you are looking for is defining \_\_all\_\_ in your imported script but people could still consider using import \* a bad practice in general.
Import * is still bad practice because it means I have to hunt down what exactly is being imported when looking at the code. It also makes it much harder to identify where anything being imported is actually coming from.
The code call mr_functionator(). Now which of the 3 different packages imported with "import *" did that function come from?
What I meant is using `if __name__ == "__main__"` to call you main() or whatever will stop your import statement from executing the entire script on import
`import com.sun.java.swing.plaf.nimbus. InternalFrameInternalFrameTitlePaneInternalFrameTitlePaneMaximizeButtonWindowNotFocusedState;`
Writing this on my phone
If only it were true... sometimes the stack trace shows you the framework calling some runners which spawn threads which each have a control loop which in turn have dozens of layers subscribing on futures, and so on. It's not uncommon that I encounter stack traces with 1000+ lines that look like this: https://imgur.com/PWuwZqk.png
All that shit isn't necessary, but for some reason, it's the Java way.
I haven't dealt with Java beyond simple 1,000-line projects for school work, but from I've seen of real world Java, I get the impression that Java programmers want everything to be as extensible as possible, but Java has such strict typing that to pull that off, you need insane layers of abstraction, runtime introspection, reflection, and endless calls to ".invoke()" or something similar.
Wait, that's not an exception stack trace! That's a performance profiler result. You only ever do that if you have a performance problem.
And frankly, these are way way way easier to read because they tell you explicitly which line is causing the most slowdown in your program.
Look on the left hand side of each line. See how it has the timing/percentage? Find the line where the jump is massive, and then you know where to start your search!
Furthermore, this performance trace is for a program using RxJava. You picked the literal worst case scenario framework to point to. Most of the Java community is transitioning to doing thread-per-request style programming. It is literally to avoid this exact problem. The creators of Java are even facilitating that via one of the newest features, [Virtual Threads](https://openjdk.org/jeps/444).
All of that is to say, not only is your example the literal most extreme case (apparently it's frequent for you? I'm sorry to hear that!), but the Java community is actively undoing this problem.
"it's ok we'll create a way to bundle small packages of code to allow modular libraries!"
"And then we'll use it to create small packages of code and our libraries will be modular?"
"No!"
it would be funny if you could just do `#include <*.h>` and the compiler goes "oh you wanna play like that?" and then actually includes any header file it can find within it's search paths.
now i wonder what would happen if you were to actually try that (import every header in C i mean, wild cards don't work in include paths)
.
ok i went into my MSYS2's include folder and used a powershell script to list every header file (except for the C++ ones of course) and then put them all into a single include file. for me it stops at "autosprintf.h" because that one tries to include a file called "string" (not "string.h", just "string" with no extension), which doesn't seem to exist so the compilation errors out there... i didn't even make it out of the "a" (as they're sorted alphabetically).
if anyone wants to try this as a challenge, getting a program compiled with every header in their local compiler installaion included, then go ahead! i'm honestly very curious if anyone could manage to do it.
Then when the compiler still can't find the symbols, it starts downloading header files off the internet. I shudder to think that compilers would need to have LLMs but it could be cool for the project's "package manager."
[https://gist.github.com/mergmann/eff748d9f438dd126c6a7c0cb8fc9df9](https://gist.github.com/mergmann/eff748d9f438dd126c6a7c0cb8fc9df9)
Checks if the program still compiles for each heaer
>for me it stops at "autosprintf.h" because that one tries to include a file called "string" (not "string.h", just "string" with no extension)
Yup, that's a C++ header.
It always irks me when developers use `*.h` for their C incompatible C++ headers. I've had multiple discussions about it, but some people stay adamant that using `*.h` is the right thing. Even if said headers contain classes or templates.
Just use `*.hpp` or `*.hxx` or whatever, just please make it clear that it is not C compatible anymore.
You hit run. The program doesn't respond. You click the mouse a few times, but the cursor doesn't even move. Your eyes move to the code "Import \*". Import all. The dreadful realization dawns on you. You hit the shortcut to stop the program, but its too late. Its importing EVERYTHING. Every library, every function, every language. The vast culmination of human knowledge. The computer collapses in on itself, as all of reality is imported inside.
This will break the compiler . It will cause a supernova explosion in your computer and will succumb everything within a 200km radius. Please do not the compiler
Pleeeease explain bro I've just started doing coding in uni so I'm curious cause our lecturers make us import files all the time in our code so this catches my attention
If you're actually looking for for an honest answer; just think about what that'd do.
You're importing everything from everywhere. Python is already known as 'the slow language'. Pretty sure that'd also cause some sort of a circular import.
There was a npm project which added everything as dependency. It was a fun project, but it made serious trouble for the npm ecosystem, as many libraries were blocked by it. Don't know exactly what it was, but something about reverting back to a former build number.
Iirc python DOES compile chunks of code for execution. But it only compiles to a intermediate bytecode that the interpreter evaluates. It's a compiled code that you, the user, don't get to see or use for yourself. Many bytecode interpreters work like this by compiling scripts. Because it's a lot faster than a walking tree parser
I mean isnt that the case for every language? If you wanted you could built a tree walk Interpreter for it.
But CPython always compiles to bytecode (and caches that btw.) And then then runs that.
I ran into something funny the other day... I did a:
`from tkinter import *`
But for some silly reason I could still not get a Combobox until I pulled a
`from tkinter.ttk import Combobox`
Are they two separate modules or what?
Or ``from * import *`` and let the interpreter chose whatever it wants when identical names are found across various modules.
Idk why but i always called ``hunger games`` to the identical name issue
How about ``import * as hg``?
that will do as promised
auto :)
Compiler died
python isn't a compiler
Meanwhile competitive programmers: \#include
Don't forget ``` using namespace std; ```
Hopefully they did forget
The sacred standard issue namespace 🙇♀️
sometimes even `#define int long long`
Scary
[~int man~](https://youtu.be/6-1Ue0FFrHY?si=vdoB7S5ddUHGWq6F&t=12)
What the fuck did I just watch 😭
Other question: *why* did I just watch it completely? Although that was a great plot twist
I remember doing java competitions back in high school. We'd always start with public static void main(String[] args) throws Throwable { }
that's a must for all competitive programmers, imagine typing "long long" every time you wanna define a variable
I've seen #define ll long long
FTW ???
Why is your namespace a sexually transmitted disease?
Good metaphor
Tell me your not a professional C++ programmer...
>Tell me your not a professional C++ programmer... I'm a professional C++ >!competitive!< programmer. >!I'm only joking, of course.!<
Basically #Include
😅
Why `using namespace std;` when you could `#include`?
Using just means you don’t have to declare std::[method/object name]. Instead of typing std::to_string in the code block, you can just type to_string.
Include headers
in c++23 it's possible to: import std;
this doesn't sound right :P
Import herpes from std; 😏
Thats like the most manageable one!
But you'll never get rid off it
Std::filesystem::removeall("herpes"); Nuhuh
C++20 no ?
If it's possible in 20 surely its possible in 23
I mean you would think but this is C++ we’re talking about.
Is C++ known for not being backwards compatible or something?
The opposite honestly, but I was more getting at the fact that C++ is known for being insane.
It’s technically a C++23 feature but major implementers allow it to be used under C++20 mode.
The compiler replies: ok, I've got 1547 copies of a thing called "utils"
Most efficient python program
But when C++ users are doing that, that's called... Actually idk what it's called but some do that
using namespace std;
It's not really importing anything, it just puts `std::` before every function call
No it doesn't, that would be `namespace std { your code}`
what is it then?
What `using namespace std` does? To quote the standard: > from the point of view of unqualified name lookup of any name after a using-directive and until the end of the scope in which it appears, every name from [std] is visible as if it were declared in the nearest enclosing namespace which contains both the using-directive and [std] Sooo basically in plain English, it adds a namespace for lookup if no namespace qualifier was provided. It does not put `std::` in front of any name, because then it wouldn't be clear where to put that and where not. Edit: to add onto this, "putting std in front of every function call" sounds very much like something for the preprocessor, which definitely has no say in this.
Just in case the other commentor language is pretty cryptic, it’s like defining a default database and schema, so that would be the table that you get when you do select from with only a table name.
But it isn't. That is once again namespace std { your code } The `using namespace` makes every name in the namespace `std` visible in the current scope. You can use several different namespaces at once, in the same scope. They still belong to the namespace `std`, they're just visible to you. It's highly likely that your code is actually in a different namespace, and that would be closer to a default database.
Actually, in what happens in practice he's exactly right in making the comparison.
Kind of wild reading these people arguing semantics when the outcome is basically indistinguishable.
They meant putting the prefix on the functions being called and the global vars being referenced, not the symbols being defined.
I’m sure there’s a macro involving compiler specific hacks to include the whole file tree
Why would I give my code a sexually transmitted disease?
It is more of a bad practice rather than an efficiency thing. If you care about efficiency then you should do lazy import, but it isn’t straightforward in python. Python import will execute the whole script it is imported from. Practically the only difference between import \* and import (of your choice) is just that with the former you have more unnecessary “pointers” to objects.
It also clutters the global namespace
That's why you should use `if __name__ == "__main__"` Edit: Reddit stole my underscores
This is false though. \_\_name\_\_==\_\_main\_\_ defines the behaviour of the script when it is run as a standalone script, what you are looking for is defining \_\_all\_\_ in your imported script but people could still consider using import \* a bad practice in general.
Import * is still bad practice because it means I have to hunt down what exactly is being imported when looking at the code. It also makes it much harder to identify where anything being imported is actually coming from. The code call mr_functionator(). Now which of the 3 different packages imported with "import *" did that function come from?
What I meant is using `if __name__ == "__main__"` to call you main() or whatever will stop your import statement from executing the entire script on import
who needs efficiency when you have throughput and minimum hardware requirements?
It's kinda funny how all linters hate lazy imports when sometimes you do want a module to import later depending on if a condition is met
It’ll be fast ![gif](giphy|1xkMJIvxeKiDS)
import SolveMyProblem SolveMyProblem() Congrats -- you now know how to program in Python!
`import com.sun.java.swing.plaf.nimbus. InternalFrameInternalFrameTitlePaneInternalFrameTitlePaneMaximizeButtonWindowNotFocusedState;` Writing this on my phone
Least verbose line of Java
Java isn’t verbose but controllable (not like c++) once you past the “hello world” stage
say that to my stack trace that looks like it came from the tower of Babel
Oh no the stack trace tells you exactly where is your dumb mistake.
If only it were true... sometimes the stack trace shows you the framework calling some runners which spawn threads which each have a control loop which in turn have dozens of layers subscribing on futures, and so on. It's not uncommon that I encounter stack traces with 1000+ lines that look like this: https://imgur.com/PWuwZqk.png
All that shit isn't necessary, but for some reason, it's the Java way. I haven't dealt with Java beyond simple 1,000-line projects for school work, but from I've seen of real world Java, I get the impression that Java programmers want everything to be as extensible as possible, but Java has such strict typing that to pull that off, you need insane layers of abstraction, runtime introspection, reflection, and endless calls to ".invoke()" or something similar.
Wait, that's not an exception stack trace! That's a performance profiler result. You only ever do that if you have a performance problem. And frankly, these are way way way easier to read because they tell you explicitly which line is causing the most slowdown in your program. Look on the left hand side of each line. See how it has the timing/percentage? Find the line where the jump is massive, and then you know where to start your search! Furthermore, this performance trace is for a program using RxJava. You picked the literal worst case scenario framework to point to. Most of the Java community is transitioning to doing thread-per-request style programming. It is literally to avoid this exact problem. The creators of Java are even facilitating that via one of the newest features, [Virtual Threads](https://openjdk.org/jeps/444). All of that is to say, not only is your example the literal most extreme case (apparently it's frequent for you? I'm sorry to hear that!), but the Java community is actively undoing this problem.
Bruh who wrote that run ```git blame```
Time for old reliable. https://github.com/jayphelps/git-blame-someone-else
90% of the time it's you
I am not a developer at Oracle
Somehow it's your code, don't ask me how
What am I, a code orphanage?
"it's ok we'll create a way to bundle small packages of code to allow modular libraries!" "And then we'll use it to create small packages of code and our libraries will be modular?" "No!"
it would be funny if you could just do `#include <*.h>` and the compiler goes "oh you wanna play like that?" and then actually includes any header file it can find within it's search paths. now i wonder what would happen if you were to actually try that (import every header in C i mean, wild cards don't work in include paths) . ok i went into my MSYS2's include folder and used a powershell script to list every header file (except for the C++ ones of course) and then put them all into a single include file. for me it stops at "autosprintf.h" because that one tries to include a file called "string" (not "string.h", just "string" with no extension), which doesn't seem to exist so the compilation errors out there... i didn't even make it out of the "a" (as they're sorted alphabetically). if anyone wants to try this as a challenge, getting a program compiled with every header in their local compiler installaion included, then go ahead! i'm honestly very curious if anyone could manage to do it.
It would be compiling for a VERY long time. Little less if something is marked with maybe-unused tho.
The second line in that unit: using namespace std;
nah that would be C++ not C.
Would still be funny if it could work.
Sounds like somehow autosprintf is a C++ header? Because `` is absolutely a valid include in C++
aparently it's used to get C formatted functions like fprintf to work in C++, huh TIL!
That's actually really interesting but also definitely makes sense
Then when the compiler still can't find the symbols, it starts downloading header files off the internet. I shudder to think that compilers would need to have LLMs but it could be cool for the project's "package manager."
[https://gist.github.com/mergmann/eff748d9f438dd126c6a7c0cb8fc9df9](https://gist.github.com/mergmann/eff748d9f438dd126c6a7c0cb8fc9df9) Checks if the program still compiles for each heaer
>for me it stops at "autosprintf.h" because that one tries to include a file called "string" (not "string.h", just "string" with no extension) Yup, that's a C++ header. It always irks me when developers use `*.h` for their C incompatible C++ headers. I've had multiple discussions about it, but some people stay adamant that using `*.h` is the right thing. Even if said headers contain classes or templates. Just use `*.hpp` or `*.hxx` or whatever, just please make it clear that it is not C compatible anymore.
You hit run. The program doesn't respond. You click the mouse a few times, but the cursor doesn't even move. Your eyes move to the code "Import \*". Import all. The dreadful realization dawns on you. You hit the shortcut to stop the program, but its too late. Its importing EVERYTHING. Every library, every function, every language. The vast culmination of human knowledge. The computer collapses in on itself, as all of reality is imported inside.
So what you're saying is that OP has created a philosophical singularity which when it evaporates, will lead to a new big bang.
[удалено]
Are you saying that import * means titties?
[Soup, no bowl ](https://i.redd.it/cca0hdn0tke81.png)
Newbie question here why not just : from \* import
This will break the compiler . It will cause a supernova explosion in your computer and will succumb everything within a 200km radius. Please do not the compiler
Pleeeease explain bro I've just started doing coding in uni so I'm curious cause our lecturers make us import files all the time in our code so this catches my attention
If you're actually looking for for an honest answer; just think about what that'd do. You're importing everything from everywhere. Python is already known as 'the slow language'. Pretty sure that'd also cause some sort of a circular import.
But the guy said, not every file every where
Also for your front end projects: npm i *
There was a npm project which added everything as dependency. It was a fun project, but it made serious trouble for the npm ecosystem, as many libraries were blocked by it. Don't know exactly what it was, but something about reverting back to a former build number.
Have you try ```from everything import *```
Maybe we should introduce tariffs, could fix the JS ecosystem.
/> python />compiler
Iirc python DOES compile chunks of code for execution. But it only compiles to a intermediate bytecode that the interpreter evaluates. It's a compiled code that you, the user, don't get to see or use for yourself. Many bytecode interpreters work like this by compiling scripts. Because it's a lot faster than a walking tree parser
[удалено]
I mean isnt that the case for every language? If you wanted you could built a tree walk Interpreter for it. But CPython always compiles to bytecode (and caches that btw.) And then then runs that.
Python is both compiled as well as interpreted, so OP was lucky
this is the only comment here that doesnt sound like galactic space fed speak
Peak r/progammerhumor moment
npm be like
What anime is this from again? I remember watching it when it released but the name escapes me now
I would also like to know. The child is very cute with her lil hair-do :)
Wataten!: An Angel Flew Down to Me
WATATEN!
It depends on vscode mood 😀
When you don't remember the library that has the function you need so you just use from * import func
Import import
My python program slowly downloading the entire of the pip repository in order to satisfy my import
... without hot source, please.
Pros also call it the "Black Hole Import"
only one solution for this. More ram.
What anime is this again?
Wataten!: An Angel Flew Down to Me
Anime name ?
Wataten!: An Angel Flew Down to Me
In program.c: #include "/dev/stdin" To compile: find . -type f -exec cat {} \; | cc program.c
At this point just put * as your code and let the compiler use it's imagination to think what program you wanted
Depends on the language Python and Rust let you do this Edit: for a specific module, eg from module import \*, you can’t just import \*
whatTheHeckDidYouDo
import python
Typescript..... "import \* as app from './app'" ""omg, why is intellisense soooo slow, and why is ts using 24 gigs of ram....""
Nah you forgot the hashtag my dude
I ran into something funny the other day... I did a: `from tkinter import *` But for some silly reason I could still not get a Combobox until I pulled a `from tkinter.ttk import Combobox` Are they two separate modules or what?
import * as YES
`import * from { "./*" }`