[Tutor] beginning to code
Chris Angelico
rosuav at gmail.com
Fri Sep 22 10:24:51 EDT 2017
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. I use the pattern all the
> time. It's called an inner class:
>
> In Python, it is possible to nest a class within another class,
> method or function.
>
> <URL: https://en.wikipedia.org/wiki/Inner_class#Programming_languages>
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.
ChrisA
More information about the Python-list
mailing list