using __init__ with params ( somewhat long )

Corey corey at axcelerant.com
Tue Jan 16 14:19:21 EST 2001


Greetings all!


I'm new to Python ( from 5 odd years of Perl ), so please bare with 
me...  

I'm not quite sure how to go about defining a "proper" Python class 
constructor, where the user can optionaly pass a parameter, or multiple 
parameters at instantiation, as a shortcut to initializing the object's
attributes in one pass, rather than using multiple 'set' methods after
instantiation.

For instance, if I'm creating a class called MyClass, with attributes
myattr1, myattr2 and myattr3, and methods set_myattr1, get_myattr1,
etc., rather than first creating a MyClass object and *then* calling 
the proper 'set' method for each attribute, I'd like to do one or both
of two things:


1 - Allow a dictionary to be passed to __init__, containing as keys
    some or all of the attributes, along with their values of course.

such as:


class MyClass:

	def __init__(self, dictionary):


		# the below could not *possibly* be considered Good(tm)
		self.__myattr1 = dictionary['myattr1']
		self.__myattr2 = dictionary['myattr2']
		self.__myattr3 = dictionary['myattr3']


attr_dict = { 'myattr1': 'goober', 'myattr3': 'lame' }

myobj = MyClass(attr_dict)

...


What if dictionary is not provided? Use an if statement first?
Make dictionary a named parameter with a default of None?

What if dictionary is provided, but does not have one of the
keys? Use more if statements to test their existance with has_key?
Use a for statement to iterate through each provided key and
define 'None' for those that weren't included?

And what if dictionary contained MyAttr1 or MYATTR1 or myaTtr1?
I would like the user to not worry about case...

My constructor seems to be getting really big at that point, just
to facilitate for some convienience to the user... 

What I'm looking for is what, if any, "standard", short-hand Pythonesque
ways of doing this would be. Like I said, I'm new to Python and
so am unfamiliar with much of it's notations. I'd rather write this
simply and elegantly, rather than by sheer, lengthy brute-force,
or to end up with something any Python programmer would look at
and wonder what the hell I was smoking when I coded it.

Also, *should* I make dictionary a named parameter, or leave it
as a floating argument?


Further, I'd also like to include the following, in addition to
point #1, above.

2 - And/or allow named arguments to be passed into __init__, one
    for each attribute.

So that we now have:
( which still runs me into the case issue )

def __init__(self, dictionary, attr1=None, attr2=None, attr3=None):


Is this considered overkill? It seems like it to me when thinking
about it, but I'm perhaps too stuck in the Perl "There's More Than 
One Way To Do It" mindset... and so want to make this class as easy 
to use as possible within the context/methodology of which it gets 
utilized.


Thanks a ton for your kind advice and insight.


Beers,

Corey






More information about the Python-list mailing list