Syntax

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • calad.sigilon@gmail.com

    #1

    Syntax

    If you use 'from os import *' then you shouldn't preface the commands
    with os.

    So, two options:

    from os import *
    print "Working Path: %s" % getcwd()

    OR

    import os
    print "Working Path: %s" % os.getcwd()

    One of these two ways you're not supposed to use for security reasons,
    but I'm spacing on which one.

  • Robert Kern

    #2
    Re: Syntax

    calad.sigilon@g mail.com wrote:[color=blue]
    > If you use 'from os import *' then you shouldn't preface the commands
    > with os.
    >
    > So, two options:
    >
    > from os import *
    > print "Working Path: %s" % getcwd()
    >
    > OR
    >
    > import os
    > print "Working Path: %s" % os.getcwd()
    >
    > One of these two ways you're not supposed to use for security reasons,
    > but I'm spacing on which one.[/color]

    I don't think there are any *security* reasons, but stylistically,
    "import os" is greatly preferred. When someone else reads your code,
    they will immediately know where getcwd() comes from.

    --
    Robert Kern
    robert.kern@gma il.com

    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter

    Comment

    • Terry Hancock

      #3
      Re: Syntax

      On Sat, 26 Nov 2005 20:54:49 -0800
      Robert Kern <robert.kern@gm ail.com> wrote:[color=blue]
      > calad.sigilon@g mail.com wrote:[color=green]
      > > So, two options:
      > >
      > > from os import *
      > > import os[/color][/color]
      [color=blue][color=green]
      > > One of these two ways you're not supposed to use for
      > > security reasons, but I'm spacing on which one.[/color]
      >
      > I don't think there are any *security* reasons, but
      > stylistically, "import os" is greatly preferred. When
      > someone else reads your code, they will immediately know
      > where getcwd() comes from.[/color]

      It's not a question of "security" in the usual sense, but
      the first syntax imports a lot of stuff into the current
      namespace, increasing the risk of unintentionally clobbering
      local names. So it's certainly "riskier" in the sense of
      "likely to cause bugs".


      --
      Terry Hancock (hancock@Anansi Spaceworks.com)
      Anansi Spaceworks http://www.AnansiSpaceworks.com

      Comment

      • Fredrik Lundh

        #4
        Re: Syntax

        Terry Hancock wrote:
        [color=blue][color=green]
        > > I don't think there are any *security* reasons, but
        > > stylistically, "import os" is greatly preferred. When
        > > someone else reads your code, they will immediately know
        > > where getcwd() comes from.[/color]
        >
        > It's not a question of "security" in the usual sense, but
        > the first syntax imports a lot of stuff into the current
        > namespace, increasing the risk of unintentionally clobbering
        > local names. So it's certainly "riskier" in the sense of
        > "likely to cause bugs".[/color]

        or just "likely to result in confusing error messages".
        [color=blue][color=green][color=darkred]
        >>> from os import *
        >>> f = open("myfile.tx t", "r")[/color][/color][/color]
        Traceback (most recent call last):
        File "<stdin>", line 1, in ?
        TypeError: an integer is required

        </F>



        Comment

        • Peter Hansen

          #5
          Re: Syntax

          Terry Hancock wrote:[color=blue][color=green]
          >>calad.sigilon @gmail.com wrote:[color=darkred]
          >>>One of these two ways you're not supposed to use for
          >>>security reasons, but I'm spacing on which one.[/color][/color]
          >
          > It's not a question of "security" in the usual sense, but
          > the first syntax imports a lot of stuff into the current
          > namespace, increasing the risk of unintentionally clobbering
          > local names. So it's certainly "riskier" in the sense of
          > "likely to cause bugs".[/color]

          There's also the case where the names which are imported are not static.
          That is, they are bound to certain objects at the time of the "import
          *" but later on they can change. While this is perhaps a sign of design
          problems in the imported module, the problem that results is that when
          those names are rebound, modules which imported them with "*" still have
          the old objects, not the new ones. Using "import module" and
          referencing things with "module.nam e" doesn't suffer from the same
          potential for problems (in addition to it being more readable etc).

          -Peter

          Comment

          Working...