Constructor re-initialization issue

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

    Constructor re-initialization issue

    Hello all,

    I have come across this issue in Python and I cannot quite understand
    what is going on.

    class Param():
    def __init__(self, data={}, condition=False ):
    if condition:
    data['class']="Advanced"
    print data

    In the previous example, I expect the variable data to be re-
    initialized every time I construct an object type Param. However, when
    I do the following:

    Param(condition =True)
    Param(condition =False)

    The second call still prints {'class': 'Advanced'}

    Shouldn't data be initialized to {} since it is the default in
    __init__? Why would the state of data be preserved between two
    independent instantiations?

    Any help would be greatly appreciated.

    M.
  • Mike Kent

    #2
    Re: Constructor re-initialization issue

    On Jun 3, 6:11 pm, pythonrept...@g mail.com wrote:
    Hello all,
    >
    I have come across this issue in Python and I cannot quite understand
    what is going on.
    >
    class Param():
    def __init__(self, data={}, condition=False ):
    if condition:
    data['class']="Advanced"
    print data
    >
    In the previous example, I expect the variable data to be re-
    initialized every time I construct an object type Param. However, when
    I do the following:
    >
    Param(condition =True)
    Param(condition =False)
    >
    The second call still prints {'class': 'Advanced'}
    >
    Shouldn't data be initialized to {} since it is the default in
    __init__? Why would the state of data be preserved between two
    independent instantiations?
    >
    Any help would be greatly appreciated.
    >
    M.
    This is a Frequently Asked Question (FAQ):
    Contents: General Python FAQ- General Information- What is Python?, What is the Python Software Foundation?, Are there copyright restrictions on the use of Python?, Why was Python created in the fi...

    Comment

    • George Sakkis

      #3
      Re: Constructor re-initialization issue

      On Jun 3, 6:11 pm, pythonrept...@g mail.com wrote:
      Hello all,
      >
      I have come across this issue in Python and I cannot quite understand
      what is going on.
      >
      class Param():
      def __init__(self, data={}, condition=False ):
      if condition:
      data['class']="Advanced"
      print data
      >
      In the previous example, I expect the variable data to be re-
      initialized every time I construct an object type Param. However, when
      I do the following:
      >
      Param(condition =True)
      Param(condition =False)
      >
      The second call still prints {'class': 'Advanced'}
      >
      Shouldn't data be initialized to {} since it is the default in
      __init__? Why would the state of data be preserved between two
      independent instantiations?
      >
      Any help would be greatly appreciated.
      >
      M.
      This must be by far the most FAQ.. unfortunately it seems it will
      remain for 3.x as well: http://www.python.org/doc/faq/genera...etween-objects

      Comment

      • pythonreptile@gmail.com

        #4
        Re: Constructor re-initialization issue

        On Jun 3, 12:59 pm, George Sakkis <george.sak...@ gmail.comwrote:
        On Jun 3, 6:11 pm, pythonrept...@g mail.com wrote:
        >
        >
        >
        Hello all,
        >
        I have come across this issue in Python and I cannot quite understand
        what is going on.
        >
        class Param():
        def __init__(self, data={}, condition=False ):
        if condition:
        data['class']="Advanced"
        print data
        >
        In the previous example, I expect the variable data to be re-
        initialized every time I construct an object type Param. However, when
        I do the following:
        >
        Param(condition =True)
        Param(condition =False)
        >
        The second call still prints {'class': 'Advanced'}
        >
        Shouldn't data be initialized to {} since it is the default in
        __init__? Why would the state of data be preserved between two
        independent instantiations?
        >
        Any help would be greatly appreciated.
        >
        M.
        >
        This must be by far the most FAQ.. unfortunately it seems it will
        remain for 3.x as well:http://www.python.org/doc/faq/genera...values-shared-...
        Thanks for clearing this up for me.

        M.

        Comment

        Working...