assign class variable in __init__

Discussion in 'Python' started by Ross Williamson, Jun 8, 2010.

  1. Hi Everyone,

    Just a quick question - Is it possible to assign class variables in
    the __init__() - i.e. somthing like:

    def __init__(self,self.source = "test", self.length = 1)

    rather than

    def __init__(self,source = "test", length = 1):


    --
    Ross Williamson
    University of Chicago
    Department of Astronomy & Astrophysics
    773-834-9785 (office)
    312-504-3051 (Cell)
    Ross Williamson, Jun 8, 2010
    #1
    1. Advertising

  2. Ross Williamson

    Peter Otten Guest

    Ross Williamson wrote:

    > Hi Everyone,
    >
    > Just a quick question - Is it possible to assign class variables in
    > the __init__() - i.e. somthing like:
    >
    > def __init__(self,self.source = "test", self.length = 1)
    >
    > rather than
    >
    > def __init__(self,source = "test", length = 1):


    No. If you are just lazy, try

    >>> import sys
    >>> def update_self():

    .... d = sys._getframe(1)
    .... d = d.f_locals
    .... self = d.pop("self")
    .... for k, v in d.iteritems():
    .... setattr(self, k, v)
    ....
    >>> class A(object):

    .... def __init__(self, source="test", length=1):
    .... update_self()
    .... def __repr__(self):
    .... return "A(source=%r, length=%r)" % (self.source,
    self.length)
    ....
    >>> A()

    A(source='test', length=1)
    >>> A(length=42)

    A(source='test', length=42)

    Personally, I prefer explicit assignments inside __init__().

    Peter
    Peter Otten, Jun 8, 2010
    #2
    1. Advertising

  3. On Jun 8, 9:37 am, Peter Otten <> wrote:
    > Ross Williamson wrote:
    > > Hi Everyone,

    >
    > > Just a quick question - Is it possible to assign class variables in
    > > the __init__() - i.e. somthing like:

    >
    > > def __init__(self,self.source = "test", self.length = 1)

    >
    > > rather than

    >
    > > def __init__(self,source = "test", length = 1):

    >
    > No. If you are just lazy, try
    >
    > >>> import sys
    > >>> def update_self():

    >
    > ...     d = sys._getframe(1)
    > ...     d = d.f_locals
    > ...     self = d.pop("self")
    > ...     for k, v in d.iteritems():
    > ...             setattr(self, k, v)
    > ...>>> class A(object):
    >
    > ...     def __init__(self, source="test", length=1):
    > ...             update_self()
    > ...     def __repr__(self):
    > ...             return "A(source=%r, length=%r)" % (self.source,
    > self.length)
    > ...>>> A()
    >
    > A(source='test', length=1)>>> A(length=42)
    >
    > A(source='test', length=42)
    >
    > Personally, I prefer explicit assignments inside __init__().
    >
    > Peter


    Or more simply

    def __init__(self, source = "test", length = 1):
    for (k, v) in locals().iteritems():
    if k != 'self':
    setattr(self, k, v)
    Jason Scheirer, Jun 8, 2010
    #3
  4. Ross Williamson

    Peter Otten Guest

    Jason Scheirer wrote:

    > On Jun 8, 9:37 am, Peter Otten <> wrote:
    >> Ross Williamson wrote:
    >> > Hi Everyone,

    >>
    >> > Just a quick question - Is it possible to assign class variables in
    >> > the __init__() - i.e. somthing like:

    >>
    >> > def __init__(self,self.source = "test", self.length = 1)

    >>
    >> > rather than

    >>
    >> > def __init__(self,source = "test", length = 1):

    >>
    >> No. If you are just lazy, try
    >>
    >> >>> import sys
    >> >>> def update_self():

    >>
    >> ... d = sys._getframe(1)
    >> ... d = d.f_locals
    >> ... self = d.pop("self")
    >> ... for k, v in d.iteritems():
    >> ... setattr(self, k, v)
    >> ...>>> class A(object):
    >>
    >> ... def __init__(self, source="test", length=1):
    >> ... update_self()
    >> ... def __repr__(self):
    >> ... return "A(source=%r, length=%r)" % (self.source,
    >> self.length)
    >> ...>>> A()
    >>
    >> A(source='test', length=1)>>> A(length=42)
    >>
    >> A(source='test', length=42)
    >>
    >> Personally, I prefer explicit assignments inside __init__().
    >>
    >> Peter

    >
    > Or more simply
    >
    > def __init__(self, source = "test", length = 1):
    > for (k, v) in locals().iteritems():
    > if k != 'self':
    > setattr(self, k, v)


    The idea was that you put update_self() into a module ready for reuse...

    Peter
    Peter Otten, Jun 8, 2010
    #4
  5. Peter Otten wrote:
    > Jason Scheirer wrote:
    >
    >
    >> On Jun 8, 9:37 am, Peter Otten <> wrote:
    >>
    >>> Ross Williamson wrote:
    >>>
    >>>> Hi Everyone,
    >>>>
    >>>> Just a quick question - Is it possible to assign class variables in
    >>>> the __init__() - i.e. somthing like:
    >>>>
    >>>> def __init__(self,self.source = "test", self.length = 1)
    >>>>
    >>>> rather than
    >>>>
    >>>> def __init__(self,source = "test", length = 1):
    >>>>
    >>> No. If you are just lazy, try
    >>>
    >>>
    >>>>>> import sys
    >>>>>> def update_self():
    >>>>>>
    >>> ... d = sys._getframe(1)
    >>> ... d = d.f_locals
    >>> ... self = d.pop("self")
    >>> ... for k, v in d.iteritems():
    >>> ... setattr(self, k, v)
    >>> ...>>> class A(object):
    >>>
    >>> ... def __init__(self, source="test", length=1):
    >>> ... update_self()
    >>> ... def __repr__(self):
    >>> ... return "A(source=%r, length=%r)" % (self.source,
    >>> self.length)
    >>> ...>>> A()
    >>>
    >>> A(source='test', length=1)>>> A(length=42)
    >>>
    >>> A(source='test', length=42)
    >>>
    >>> Personally, I prefer explicit assignments inside __init__().
    >>>
    >>> Peter
    >>>

    >> Or more simply
    >>
    >> def __init__(self, source = "test", length = 1):
    >> for (k, v) in locals().iteritems():
    >> if k != 'self':
    >> setattr(self, k, v)
    >>

    >
    > The idea was that you put update_self() into a module ready for reuse...
    >
    > Peter
    >

    still

    def __init__(self, source="test", length=1):
    self.source = source
    self.length = length

    is the way to go. OP's original idea is a bad idea :).
    Could be a problem with hundreds of parameters, but who write
    constructors with hundreds of parameters ?

    JM
    Jean-Michel Pichavant, Jun 8, 2010
    #5
  6. Ross Williamson

    Peter Otten Guest

    Jean-Michel Pichavant wrote:

    > def __init__(self, source="test", length=1):
    > self.source = source
    > self.length = length
    >
    > is the way to go. OP's original idea is a bad idea :).


    D'accord.

    > Could be a problem with hundreds of parameters, but who write
    > constructors with hundreds of parameters ?
    Peter Otten, Jun 8, 2010
    #6
  7. Ross Williamson

    danieldelay Guest

    you can also use :

    >>> class A(object):

    def __init__(self, **args):
    self.__dict__.update(args)

    >>> a=A(source='test', length=2)
    >>> a.source

    'test'

    but this is to be used carefully because mispelling your args somewhere
    in your program will not raise any error :
    >>> A(sourc='test', lenth=2)


    Daniel
    danieldelay, Jun 8, 2010
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    8
    Views:
    53,487
    saravanavsk
    Jan 25, 2010
  2. Steven Bethard
    Replies:
    2
    Views:
    445
    Steven Bethard
    Feb 16, 2005
  3. Kent Johnson
    Replies:
    7
    Views:
    883
    Jan Niklas Fingerle
    Feb 12, 2006
  4. Mark Lawrence

    Re: assign class variable in __init__

    Mark Lawrence, Jun 8, 2010, in forum: Python
    Replies:
    3
    Views:
    282
    James Mills
    Jun 8, 2010
  5. Ramchandra Apte
    Replies:
    17
    Views:
    324
    Manuel Pégourié-Gonnard
    Sep 30, 2012
Loading...

Share This Page