create instance attributes for every method argument

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Berco Beute

    create instance attributes for every method argument

    I remember reading somewhere how to create an instance attribute for
    every method argument, but although Google is my friend, I can't seem
    to find it. This could likely be done way more elegant:

    =============== ==========
    class Test(object):

    def __init__(self, a, b, c, d, e, f):
    self.a = a
    self.b = b
    self.c = c
    self.d = d
    =============== ==========

    2B
  • Larry Bates

    #2
    Re: create instance attributes for every method argument

    Berco Beute wrote:
    I remember reading somewhere how to create an instance attribute for
    every method argument, but although Google is my friend, I can't seem
    to find it. This could likely be done way more elegant:
    >
    =============== ==========
    class Test(object):
    >
    def __init__(self, a, b, c, d, e, f):
    self.a = a
    self.b = b
    self.c = c
    self.d = d
    =============== ==========
    >
    2B
    IMHO you can't do much better than that with positional arguments, but you can
    if they are keyword arguments.

    class foo(object):
    def __init__(self, **kwargs):
    self.__dict__.u pdate(kwargs)

    -Larry

    Comment

    • Duncan Booth

      #3
      Re: create instance attributes for every method argument

      Berco Beute <cyberco@gmail. comwrote:
      I remember reading somewhere how to create an instance attribute for
      every method argument, but although Google is my friend, I can't seem
      to find it. This could likely be done way more elegant:
      >
      >============== ===========
      class Test(object):
      >
      def __init__(self, a, b, c, d, e, f):
      self.a = a
      self.b = b
      self.c = c
      self.d = d
      >============== ===========
      >
      2B
      You *could* do something like this:
      >>class Test(object):
      def __init__(self, a, b, c, d, e, f):
      self.update(loc als())

      def update(self, adict):
      for k in adict:
      if k != 'self':
      setattr(self, k, adict[k])

      >>c = Test(1, 2, 3, 4, 5, 6)
      >>c.__dict__
      {'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4, 'f': 6}
      >>>
      but to be honest, your original is much clearer as it expresses the
      intention without any obfuscation and as soon as you want to do anything
      more than simply copying all arguments you'll want to do the assignments
      individually anyway.

      Comment

      • Peter Otten

        #4
        Re: create instance attributes for every method argument

        Berco Beute wrote:
        I remember reading somewhere how to create an instance attribute for
        every method argument, but although Google is my friend, I can't seem
        to find it. This could likely be done way more elegant:
        >
        =============== ==========
        class Test(object):
        >
        def __init__(self, a, b, c, d, e, f):
        self.a = a
        self.b = b
        self.c = c
        self.d = d
        =============== ==========


        Personally, I prefer to spell it out like you did above.

        Peter

        Comment

        Working...