To begin in D coming from Python
Luis P. Mendes
luislupeXXXX at gmailXXXX.com
Mon Jul 21 11:43:22 PDT 2008
Mon, 21 Jul 2008 13:10:54 -0400, bearophile wrote:
>>I wanted to know if I can load values for attributes in a concise
>>manner.<
>
> Can you show a working Python example of what you mean? So I/we can
> avoid guessing many times.
Sure, for example:
self.lista_campos: ['indice', 'emac_periodos', 'emal_bool',
'emal_periodos', 'tr1_bool', 'tr1_periodos', 'tr1_sinal', 'tr1_percent',
'tr2_bool', 'tr2_periodos', 'tr2_sinal', 'tr2_percent', 'tr2_vezes']
individuo_sql: (2264, 42, True, 88, True, 15, '>', Decimal("0.49"),
False, 6, '<', Decimal("0.84"), 4]
for atributo, valor in zip(self.lista_campos, individuo_sql): setattr
(self, atributo, valor)
so that:
print self.indice
2264
print self.emac_periodos
42
and so on.
In this one I also used a zip function to pack values from two lists
together.
>
>
>>For example, right now I'm working on a project that reads and writes
>>circa 40 values for each instance of an object and in Python I'm able to
>>load the values on a for loop as mentioned above.<
>
> For example objects and structs have the .tupleof that allows you to
> refer to attributes as items of a untyped array. And for example in my
> libs you can find HasMethod!() and autoAssign(), that can be used like
> (I don't use them much, but they are there, I'll prune away little used
> stuff in the future, I'm in the inflation phase still):
>
> assert( HasMethod!(__LINE__, MyClass, "foo", int, float) );
>
> assert( HasMethod!(__LINE__, MyClass, "foo") );
>
> (I'd like to remove that __LINE__).
>
> // And:
> this(int first, int second, int third) {
> mixin(autoAssign("first second third"));
> }
>
> // Is the same as:
> this(int first, int second, int third) {
> this.first = first;
> this.second = second;
> this.third = third;
> }
>
> So if you know attribute names at compile time you probably need "just"
> some template trickery to solve your problem (but despite D templates
> being simpler than C++ ones it may require you some time to learn to use
> them to perform such tricks).
Yes, I guess so! :-)
Luis
More information about the Digitalmars-d
mailing list