list comprehension namespace problem

Chris Angelico rosuav at gmail.com
Fri Sep 25 01:46:43 EDT 2020


On Fri, Sep 25, 2020 at 3:43 PM Frank Millman <frank at chagford.com> wrote:
>
> Hi all
>
> I have a problem related (I think) to list comprehension namespaces. I
> don't understand it enough to figure out a solution.
>
> In the debugger, I want to examine the contents of the current instance,
> so I can type
>
>      (Pdb) dir(self)
>
> and get the result with no problem.
>
> However, it is a long list containing attribute names and method names,
> and I only want to see the attribute names. So I tried this -
>
>      (Pdb) [x for x in dir(self) if not callable(getattr(self, x))]
>      *** NameError: name 'self' is not defined
>      (Pdb)
>
> Q1. Can someone explain what is going on?
>
> Q2. Is there a way to get what I want?
>

If you put that line of code into your actual source code, does it
work? I think this might be a pdb-specific issue, since normally the
comprehension should have no difficulty seeing names from its
surrounding context.

A minimal case will probably involve the debugger and a function with
a local, unless in some way this depends on 'self' being special.

ChrisA


More information about the Python-list mailing list