assignment in if

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gary Wessle

    #1

    assignment in if

    Hi

    is there a way to make an assignment in the condition of "if" and use
    it later, e.g.

    nx = re.compile('reg ex')
    if nx.search(text) :
    funCall(text, nx.search(text) )

    nx.search(text) is evaluated twice, I was hoping for something like

    nx = re.compile('reg ex')
    if x = nx.search(text) :
    funCall(text, x))

    thanks
  • Roy Smith

    #2
    Re: assignment in if

    In article <87iroodnqz.fsf @localhost.loca ldomain>,
    Gary Wessle <phddas@yahoo.c om> wrote:
    [color=blue]
    > Hi
    >
    > is there a way to make an assignment in the condition of "if" and use
    > it later, e.g.
    >
    > nx = re.compile('reg ex')
    > if nx.search(text) :
    > funCall(text, nx.search(text) )
    >
    > nx.search(text) is evaluated twice, I was hoping for something like
    >
    > nx = re.compile('reg ex')
    > if x = nx.search(text) :
    > funCall(text, x))[/color]

    Personally, I find the C-style idiom you long for to be convenient and
    useful. That being said, it does not exist in Python, by deliberate design
    decision. In Python, assignment is not an operator with side effects like
    in C or Java, but a statement. What you need to do is:

    nx = re.compile('reg ex')
    x = nx.search(text)
    if x:
    funCall(text, x))

    The lack of embedded assignments leads to slightly more verbose code in
    situations like this, but on the other hand, it avoids the typical C
    disaster of writing a whole function as a one liner.

    Comment

    • Bruno Desthuilliers

      #3
      Re: assignment in if

      Gary Wessle a écrit :[color=blue]
      > Hi
      >
      > is there a way to make an assignment in the condition of "if" and use
      > it later[/color]

      No.
      [color=blue]
      >, e.g.
      >
      > nx = re.compile('reg ex')
      > if nx.search(text) :
      > funCall(text, nx.search(text) )
      >
      > nx.search(text) is evaluated twice, I was hoping for something like
      >
      > nx = re.compile('reg ex')
      > if x = nx.search(text) :
      > funCall(text, x))[/color]

      x = nx.search(text)
      if x:
      funCall(text, x)

      Comment

      • Ben Caradoc-Davies

        #4
        Re: assignment in if

        Gary Wessle wrote:[color=blue]
        > is there a way to make an assignment in the condition of "if"[/color]

        No.
        [color=blue]
        > nx = re.compile('reg ex')
        > if x = nx.search(text) :
        > funCall(text, x))[/color]

        Use:

        nx = re.compile('reg ex')
        x = nx.search(text)
        if x:
        funCall(text, x)

        --
        Ben Caradoc-Davies <ben@wintersun. org>
        A milestone document in the history of human rights, the Universal Declaration of Human Rights set out, for the first time, fundamental human rights to be universally protected. It has been translated into over 500 languages.

        "Those who deny freedom to others deserve it not for themselves."
        - Abraham Lincoln

        Comment

        • Edward Elliott

          #5
          Re: assignment in if

          Roy Smith wrote:[color=blue]
          > decision. In Python, assignment is not an operator with side effects like
          > in C or Java, but a statement.[/color]

          <nitpick> assignemnt is actually an expression in those languages, not a
          statement. </nitpick>
          [color=blue]
          > The lack of embedded assignments leads to slightly more verbose code in
          > situations like this, but on the other hand, it avoids the typical C
          > disaster of writing a whole function as a one liner.[/color]

          Writing disasters in python just takes a little more creativity. ;)

          Comment

          • Bruno Desthuilliers

            #6
            Re: assignment in if

            Edward Elliott a écrit :[color=blue]
            > Roy Smith wrote:
            >[/color]
            (snip)
            [color=blue][color=green]
            >>The lack of embedded assignments leads to slightly more verbose code in
            >>situations like this, but on the other hand, it avoids the typical C
            >>disaster of writing a whole function as a one liner.[/color]
            >
            > Writing disasters in python just takes a little more creativity. ;)
            >[/color]
            +1 QOTW

            Comment

            • Edward Elliott

              #7
              Re: assignment in if

              Edward Elliott wrote:[color=blue]
              > <nitpick> assignemnt is actually an expression in those languages, not a
              > statement. </nitpick>[/color]

              s/statement/operator/


              Comment

              • Edward Elliott

                #8
                Re: assignment in if

                Edward Elliott wrote:[color=blue]
                > Edward Elliott wrote:[color=green]
                >> <nitpick> assignemnt is actually an expression in those languages, not a
                >> statement. </nitpick>[/color]
                >
                > s/statement/operator/[/color]

                <shakes head>
                it was probably clearer the first time, but let me rephrase:
                in C/Java, assignment is neither a statement not an operator. it is an
                expression involving a certain operator. that operator (not so)
                coincidentally happens to be the assignment operator, or one of its
                variants.

                ah forget it, replying to your reply to yourself is probably illegal...

                Comment

                Working...