Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > assign class variable in __init__

Reply
Thread Tools

assign class variable in __init__

 
 
Ross Williamson
Guest
Posts: n/a
 
      06-08-2010
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)
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      06-08-2010
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
 
Reply With Quote
 
 
 
 
Jason Scheirer
Guest
Posts: n/a
 
      06-08-2010
On Jun 8, 9:37*am, Peter Otten <(E-Mail Removed)> 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)
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      06-08-2010
Jason Scheirer wrote:

> On Jun 8, 9:37 am, Peter Otten <(E-Mail Removed)> 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
 
Reply With Quote
 
Jean-Michel Pichavant
Guest
Posts: n/a
 
      06-08-2010
Peter Otten wrote:
> Jason Scheirer wrote:
>
>
>> On Jun 8, 9:37 am, Peter Otten <(E-Mail Removed)> 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
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      06-08-2010
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 ?



 
Reply With Quote
 
danieldelay
Guest
Posts: n/a
 
      06-08-2010
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Should one always add super().__init__() to the __init__? Ramchandra Apte Python 17 09-30-2012 12:04 PM
Re: assign class variable in __init__ Mark Lawrence Python 3 06-08-2010 08:41 PM
assign JavaScript variable to Java variable problem in JSP Matt Java 8 01-25-2010 04:48 PM
super(...).__init__() vs Base.__init__(self) Kent Johnson Python 7 02-12-2006 08:59 PM
__new__ does not call __init__ as described in descrintro.html (WAS:Can __new__ prevent __init__ from being called?) Steven Bethard Python 2 02-16-2005 06:50 AM



Advertisments