Reputation: 39
The code:
from os import path
path.__dict__['os'].system("/bin/sh")
Allows me to make a shell in python. I am curious why the os module can be accessed through the path class, when I didn't explicitly import the entire os module. I have read a few articles on dict, and how it stores the variables, methods, to the class etc. But I didn't find that it would keep the module it came from.
Upvotes: 1
Views: 75
Reputation: 251478
It doesn't have anything special to do with __dict__
; you can also get it as just path.os
. The os.path
module imports the os
module. That means that the name os
is accessible from os.path
. Anytime a module does import foo
, you can access foo
via that module just like you'd access anything else in the module. Modules are just normal objects, and imported modules are accessible just like classes or functions or anything else in a module.
Upvotes: 2