- E04 - Leadership! Google, Guido van Rossum, PSF
Ilias Lazaridis
ilias at lazaridis.com
Wed Jan 4 08:00:30 EST 2006
Alex Martelli wrote:
> Ilias Lazaridis <ilias at lazaridis.com> wrote:
[...]
>>possibly one can provide the code for something similar to the ruby
>>attr_accessor:
>>
>>class Talker
>> def sayHello
>> puts "Hello world"
>> end
>>
>> attr_accessor :name, :age
>>
>>end
>>
>>thus they can later be accessed this way
>>
>>john.age = 19
>>
>>print john.age
[...]
> Yes: the amount of code one needs to provide for this purpose is, NONE.
[...]
> just as you request. _No need to provide ANY code in the class_.
[...] - (elaborations on getter/setter need)
I've understood your elaborations.
I would need this python "attr_accessor", to showcase that python is
capable to do it (even if the usage seems irrational/redundant).
>>>instance and introspecting on it. Would such instantiation be OK here?
>>
>>If I understand you right, it would be ok.
>>
>>The requirements are given by the template:
>>
>>john.sayYourClassDefinition()
>>
>>"john" is instantiated, when asked for his class definition.
>
> OK, I'll look into that in my copious spare time (unless somebody else
> does the work first;-).
would be very nice!
>>>>"assign to it" with:
>>>>
>>>>setattr(Talker, 'meta', "Class meta information")
[...]
>>>>Talker.meta = "Class meta information"
[...]
>>But this means that "assignment of metadata" works fine.
>
> Yes, on mutable objects such as ordinary classes it does work fine. It
> would not work on immutable objects such as strings or numbers.
ok
>>>>>>class Talker(object): pass
>>>>>>Talker.meta = 'class metainfo'
>>>>>>print Talker.meta
>>>
>>>class metainfo
>>
>>thus if I make a typo, I create a new attribute?
>
> Yep, just like, I believe, in Ruby; if you meant to assign to @zappo but
> happen to mistakenly assign to @zippo instead, you've created a new
> attribute. In Python, if you wish, you can check for such "oops"-level
[...] - (elaborations on code-verification / unit-testing)
Ok.
.
--
http://lazaridis.com
More information about the Python-list
mailing list