Ruby Impressions (fwd) (fwd) 
Author Message
 Ruby Impressions (fwd) (fwd)

| class Person:
|     def __init__(self, name, age, gender):
|         self.name, self.age, self.gender = name, age, gender
|         self.doSomeStuff()
|I'd like to be able to tell Python, "Make me an __init__ method that
|takes these three parameters, stores them as attributes, and then does
|this other stuff."

Like this?

  >>> class Person:
  ...     def __init__(self, **args):
  ...         self.__dict__.update(args)
  ...
  >>> p = Person(name='Joe',age=37,gender='M/F')
  >>> p.name
  'Joe'



Wed, 30 Jun 2004 15:42:56 GMT  
 Ruby Impressions (fwd) (fwd)



Quote:

> | class Person:
> |     def __init__(self, name, age, gender):
> |         self.name, self.age, self.gender = name, age, gender
> |         self.doSomeStuff()
> |I'd like to be able to tell Python, "Make me an __init__ method
that
> |takes these three parameters, stores them as attributes, and then
does
> |this other stuff."

> Like this?

>   >>> class Person:
>   ...     def __init__(self, **args):
>   ...         self.__dict__.update(args)
>   ...
>   >>> p = Person(name='Joe',age=37,gender='M/F')
>   >>> p.name
>   'Joe'

The price of this simplicity is loss of control as to what attributes
get stored.

Adam, you actually could write a function make_init(class,
list_of_attrs, other_stuff) that would write the code and exec it.
However, this would work better as part of a python development
environment than as part of run-time code.

Terry J. Reedy



Thu, 01 Jul 2004 00:52:41 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. (Fwd) (Fwd) Re: Band Matrices

2. (Fwd) MESSAGE ON PHONE SCAM (fwd)

3. [Fwd: envienla (fwd)]

4. Fwd: [LogoForum] Re: (Fwd) [apcs] Karel Goes Fishing!

5. Fwd: NetRexx language now available (fwd)

6. [Fwd: Re: RubyInline 1.0.4 Released! (fwd)]

7. There's got to be an easy way to do this (fwd) (fwd)

8. utility to display the contents of a pickle (fwd) (fwd)

9. get-a-cup-of-coffee slow (fwd) (fwd)

10. Announcing DOS port availability (fwd) (Was: Announcing DOS port availability (fwd))

11. LOGO-L> (Fwd) Fwd: (fwd) Computers and education

12. Is 0 > None?? (fwd) (fwd)

 

 
Powered by phpBB® Forum Software