new to python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Patton

    #1

    new to python

    I need to make several user-defined objects for a program I'm making.
    I know that one way is to use the exec statement (i.e. exec
    'raw_input() = "cheese"'), but it can get very visually cluttered at
    times.
    i.e. exec raw_input()+num +'={"I":'+b+',' +am+':"just","f ine":1}'

    Does anyone know a different way I can do this? I need to do this to
    define a dictionary, but a general method for all objects would be
    better.

    Thanks for any help!
    ---
  • Mitja

    #2
    Re: new to python

    Chris Patton wrote:[color=blue]
    > I need to make several user-defined objects for a program
    > I'm making. I know that one way is to use the exec
    > statement (i.e. exec 'raw_input() = "cheese"'), but it
    > can get very visually cluttered at times.
    > i.e. exec
    > raw_input()+num +'={"I":'+b+',' +am+':"just","f ine":1}'
    >
    > Does anyone know a different way I can do this? I need to
    > do this to define a dictionary, but a general method for
    > all objects would be better.[/color]

    Don't go for global user-defined variables - it's hard to control and a big security risk. A far better thing to do would be
    something like

    usersVars={}
    usersVars[raw_input()]= {4:'sdf', 'foo':'bar'}


    Comment

    • Michael Hoffman

      #3
      Re: new to python

      Chris Patton wrote:
      [color=blue]
      > I need to make several user-defined objects for a program I'm making.
      > I know that one way is to use the exec statement (i.e. exec
      > 'raw_input() = "cheese"'), but it can get very visually cluttered at
      > times.
      > i.e. exec raw_input()+num +'={"I":'+b+',' +am+':"just","f ine":1}'[/color]

      Hi Chris. Your question has been discussed by several people already the
      last time you answered it. Please do us the courtesy of reading those
      answers before repeating your question.

      Also, my short answer to your question is: don't do that! I really have
      to question why you need to set variables in local or global scope
      rather than just setting items in a dictionary. Why?
      --
      Michael Hoffman

      Comment

      Working...