user2827678
user2827678

Reputation: 39

Why can you access the os module from path.__dict__ in python?

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

Answers (1)

BrenBarn
BrenBarn

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

Related Questions