[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Tutor] beginning to code

Chris Angelico <rosuav at gmail.com>:

> On Fri, Sep 22, 2017 at 10:26 PM, Marko Rauhamaa <marko at pacujo.net> wrote:
>> Chris Angelico <rosuav at gmail.com>:
>>> (Side point: Your slot_ref function is rather bizarre. It's a closure
>>> AND a class, just in case one of them isn't sufficient.
>> I don't see anything bizarre in it at all.

> Sure you *can* do it. It's perfectly legal Python syntax. But what's
> the point? The class alone will do it.
> class slot_ref:
>     def __init__(self, ns, key):
>         self.ns = ns; self.key = key
>     def get(self): return self.ns[self.key]
>     def set(self, value): self.ns[self.key] = value
> That should be drop-in compatible with your original function/class
> hybrid, complete with following the naming convention for functions
> rather than classes.

You are right. That one would be the way to go here.