Re: class declaration shortcut



Luis M. González wrote:
On Feb 28, 6:21 pm, Steven Bethard <steven.beth...@xxxxxxxxx> wrote:
How about something like::

class Person(Record):
__slots__ = 'name', 'birthday', 'children'

You can then use the class like::

person = Person('Steve', 'April 25', [])
assert person.name == 'Steve'
assert person.birthday == 'April 25'
assert not person.children

Is that what you were looking for? If so, the recipe for the Record
class is here:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/502237
[snip]
Hmmm... not really.
The code above is supposed to be a shorter way of writing this:

class Person:
def __init__(self, name, birthday, children):
self.name = name
self.birthday = birthday
self.children = children

So the purpose of this question is finding a way to emulate this with
a single line and minimal typing.

That __init__ is exactly what was generated in my example above. So you're mainly objecting to using two-lines? You can make it a one-liner by writing::

class Person(Record): __slots__ = 'name', 'birthday', 'children'

1) How to get the variable name (in this case "Person") become the
name of the class without explicity indicating it.

The only things that know about their own names are class statements (through metaclasses) so you can't really do it without a class statement of some sort (which means you'll have to use two lines).

2) How to enter attribute names not enclosed between quotes. The only
way I can do it is by entering them as string literals.

If you're really bothered by quotes, a pretty minimal modification to the recipe could generate the same code from:

class Person(Record): slots = 'name birthday children'

STeVe
.



Relevant Pages

  • Re: Rowling asserts her rights
    ... Is it some legalese phrase with a well defined ... :>> legal meaning? ... What I am wondering is why did she feel she needed to assert them? ... It's a matter of legal protection if the assertion is in writing. ...
    (alt.fan.harry-potter)
  • Re: segmentation fault, malloc error
    ... overwrite part of memory somewhere (by writing out of array bounds) and then ... but I guess you haven't used any assert() ... those are very useful to track down memory faults. ...
    (comp.lang.c)
  • Re: New CD Is Just Awful!!!
    ... They comment, assert, ... question, opine, suggest. ... Instead of writing new words to Woody Guthrie tunes, ... new words to the classics of Chicago blues. ...
    (rec.music.dylan)
  • Re: How come C allow structure members to be addressed like an array ?
    ... A very verbose way of writing ... Tomás's code is over complex and could fail in ... Or even a compile time assert: ... Saying that, the address test could be evaluated at compile time, so the ...
    (comp.lang.c)
  • Re: How does assert benefit your code really?
    ... fix the bug in the program, an assert() is likely to be appropriate. ... although I've used the equivalent in assembler (if the hardware screws ... few scanlines on a page which has very few anyway, ... I guess that writing drivers without ...
    (comp.lang.c)