Contexts for If/Loops

Andreas Pakulat apaku at gmx.de
Tue Aug 28 22:26:41 UTC 2007


On 27.08.07 21:17:03, Eike Welk wrote:
> On Thursday 23 August 2007 14:26, Alexander Dymo wrote:
> > On 8/23/07, Andreas Pakulat <apaku at gmx.de> wrote:
> > > On 23.08.07 03:56:46, Piyush Verma wrote:
> > > > Is it valid/usable to have separate contexts for the IF /
> > > > while/ For/ try blocks in Python.?
> > >
> > > No only Python but also C++, so far I don't think this is
> > > happening for C++ and I wonder how code-completion works for
> > > objects created inside an if-block...
> >
> > My understanding of contexts was that it is roughly the "{}" block
> > in c++ code - the scope where variables are defined. If that's true
> > then if/while/for ... should have their own contexts.
> >
> > But for ruby for example the variable first used inside
> > if/while/etc. is still declared/defined after the statement:
> >
> > if something
> >    a = 2
> > end
> > #here "a" is defined and has either value nil or 2 depending on
> > whether something false or true
> > puts a
> >
> > So I wonder whether python has the same rule.
> 
> Yes, in Python it works exactly the same:

No, not quite. In Python any variable defined inside the if block is
visible on the outside, so

def test1():
  if True:
    a=1
    b=2
  print a,b

test1()

works as well. So that could create some problems, unless duchain
allows to easily and fast search to those contexts, recursively.

Andreas

-- 
You are the only person to ever get this message.




More information about the KDevelop-devel mailing list