I guess this feature is mainly useful for debugging since it is really hard to do it consistantly in python. And IMHO, for debuging, it might be more useful to record the position (file + line num etc) where the object is created, which has less ambiguity and can probably already be done by tracing back the call stack in the constructor (until the most derived constructor or even just record the whole call stack).
For not writing the name of a named tuple twice, won't it be a better idea to just use it as a base class, i.e.: class TupleName(named_tuple_base('a', 'b', 'c', 'd')): pass or maybe even using the syntax and trick of the new Enum class introduced in python 3.4(? or 3.3?) class TupleName(TupleBase): a = 1 b = 1 IMHO, this fits the python syntax better (if the current one is not good enough :) ) Yichao Yu On Wed, Jul 16, 2014 at 10:03 AM, Steven D'Aprano <st...@pearwood.info> wrote: > On Tue, Jul 15, 2014 at 5:05 PM, anatoly techtonik <techto...@gmail.com> > wrote: > >> Is it possible at all to define a class in Python that >> can read name of variable it is assigned to on init? >> >> >>> MyObject = SomeClass() >> >>> print(MyObject) >> 'MyObject' > > This feature would be useful for things like namedtuple, where we > currently have to write the name twice: > > record = namedtuple('record', 'a b c d') > > But I'm not sure why Anatoly is asking here. It would be a change in > semantics of Python, and while I suppose it's possible for PyPy to lead > the way with a semantic change for Python 3.5 or higher, or even an > implementation-specific feature that other Python's don't offer, I would > expect that normally this idea should go through CPython first. > > -- > Steven > _______________________________________________ > pypy-dev mailing list > pypy-dev@python.org > https://mail.python.org/mailman/listinfo/pypy-dev _______________________________________________ pypy-dev mailing list pypy-dev@python.org https://mail.python.org/mailman/listinfo/pypy-dev