How 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.

1 Like

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.

1 Like

I never used namespace thing before. So I’m not certain how to give a shot. Appreciate your help.

1 Like

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”.

1 Like

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?

1 Like