assign class variable in __init__

  • Thread starter Ross Williamson
  • Start date
R

Ross Williamson

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)
 
P

Peter Otten

Ross said:
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
.... d = sys._getframe(1)
.... d = d.f_locals
.... self = d.pop("self")
.... for k, v in d.iteritems():
.... setattr(self, k, v)
........ def __init__(self, source="test", length=1):
.... update_self()
.... def __repr__(self):
.... return "A(source=%r, length=%r)" % (self.source,
self.length)
....A(source='test', length=42)

Personally, I prefer explicit assignments inside __init__().

Peter
 
J

Jason Scheirer

No. If you are just lazy, try


...     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)
 
P

Peter Otten

Jason said:
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
 
J

Jean-Michel Pichavant

Peter said:
Jason Scheirer wrote:



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
 
P

Peter Otten

Jean-Michel Pichavant said:
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 ?
 
D

danieldelay

you can also use :
def __init__(self, **args):
self.__dict__.update(args)
'test'

but this is to be used carefully because mispelling your args somewhere
in your program will not raise any error :
Daniel
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top