a = 10
b = 3.14
s = ‘Hello’
x = True
Now I want to display all the objects defined in the current namespace by using the ‘dir’ function.
a = 10
b = 3.14
s = ‘Hello’
x = True
Now I want to display all the objects defined in the current namespace by using the ‘dir’ function.
Hi David,
This also sounds like a homework exercise. What have you tried?
Hint: you know the function is called dir. Do you know how to call a
function using parentheses (or round brackets, if you prefer that name)?
What happens if you call the dir function?
Note that the behaviour will be a little bit different in an interactive
interpreter, where you have the >>> prompt and can type commands and
have their executed immediately, and in a script.
I never used namespace thing before. So I’m not certain how to give a shot. Appreciate your help.
Be brave! Try something. The worst that happens is you’ll get an error
message and you will have learned what doesn’t work.
Reminder: the builtin function is called dir. To call a function put
parentheses after it, like this: dir(). What happens when you try it?
If you’re still worried about trying it, read the documentation first:
https://docs.python.org/3/library/functions.html#dir
Hint: “local scope” is another term for “current namespace”.
I tried like,
class temp:
def dir(self)
a=10
b=6
c=‘Hello’
s=temp()
dir(s)
It is not working. Not sure where I made a mistake. May I request you to help me?
this code works man check the indentations and execute it
def fun():
a=10
b=3.14
s=‘Hello’
x=True
print(dir())
fun()
The answer is
we need to define a class to execute dir() function
class Person:
a=10
b=3.14
s=“Hello”
x=True
print(dir(Person))
If we write coding like this it will print namespaces of the object.
Try this
a=10
b=3.14
s=‘Hello’
x=True
print(dir())
def fun():
a = 10
b = 3.14
s = ‘Hello’
x = True
print(dir())
fun()
output:
[‘a’, ‘b’, ‘s’, ‘x’]
Thanks man! This really worked…

Thank you for encouraging to understand the concept. I was trying to solve the same. And I did it myself, thanks to your advice.
for name in dir():
value = globals()[name]
print(name, type(value), value)