Generic Interface

A flexible interface for any class would be when

For example:

 All parameters and parameter names are put in an array.
  a=array('Username'->'peter','password'->'mypassword','e_mail'->'peter@nowhere.com');

An object is initialized like.
  user.init(a)

The idea is that any object can be initialized by the same function init() with an array a as atribute.

(this is a draft page) please add some ideas, fix my poor english


Python functions often do this. The keyword arguments may also be mixed with normal arguments and default values for the normal arguments, e.g.:

  >>> def foo(first=True, **kwargs):
  ...     print "%s, %s" % (str(first), str(kwargs))
  ...
  >>> foo()
  True, {}
  >>> foo(False)
  False, {}
  >>> foo(foo=1)
  True, {'foo': 1}
  >>> foo(foo=1, bar=2)
  True, {'foo': 1, 'bar': 2}
  >>> foo(False, foo=1, bar=2)
  False, {'foo': 1, 'bar': 2}

The kwargs name refers to a dictionary, and dictionaries support default values for non-existent keys. This makes it possible to write code like:

  def foo(**kwargs):
      name = kwargs.get('name', "Default User")
      ...

This is a relatively clean way of writing semi-polymorphic functions that support an arbitrary number of key=value pairs.


EditText of this page (last edited April 12, 2010) or FindPage with title or text search