<div dir="ltr">I think the documentation is correct but you misinterpreted the intent of that code. The code you're quoting, which is an example, is not about ending up with a custom dict within the instance, the intent of the author was just to captur the memeber_names list. So what it does for that is customizing the class dict in prepare(), but then in __init__ it *intentionally* converts it to a regular dict after extracting the member_names. The goal of the example is ending up with instances with regular attribute dicts but an extra member_names attributes, while I think that you're looking for to end up with a custom attribute dict (so in *your* case, you do not need to do the copying)</div><br><div class="gmail_quote"><div dir="ltr">On Sun, 4 Nov 2018 at 00:03, Amit Green <<a href="mailto:amit.mixie@gmail.com">amit.mixie@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">
<div dir="ltr">Thanks Daniel,</div><div dir="ltr"><br></div><div style="margin-left:40px"> I found my answer here (using your link): <a href="https://docs.python.org/3/reference/datamodel.html#preparing-the-class-namespace" target="_blank">https://docs.python.org/3/reference/datamodel.html#preparing-the-class-namespace</a></div><div dir="ltr"><br></div><div style="margin-left:40px">"""</div><div style="margin-left:40px">
When a new class is created by <code class="m_7342312465347859571gmail-docutils m_7342312465347859571gmail-literal m_7342312465347859571gmail-notranslate"><span class="m_7342312465347859571gmail-pre">type.__new__</span></code>, the object provided as the
namespace parameter is copied to a new ordered mapping and the original
object is discarded. <br></div><div style="margin-left:40px">"""</div><div style="margin-left:40px"><br></div><div style="margin-left:40px">Therefore the answer seems to be that 
<a href="https://www.python.org/dev/peps/pep-3115/" target="_blank">https://www.python.org/dev/peps/pep-3115/</a> needs to be updated & fixed.</div><div style="margin-left:40px"><br></div><div style="margin-left:40px">Replace the following:</div><div style="margin-left:40px"><br></div><div style="margin-left:40px">"""</div><div style="margin-left:40px">
<pre class="m_7342312465347859571gmail-literal-block">    def __new__(cls, name, bases, classdict):
        # Note that we replace the classdict with a regular
        # dict before passing it to the superclass, so that we
        # don't continue to record member names after the class
        # has been created.
        result = type.__new__(cls, name, bases, dict(classdict))
        result.member_names = classdict.member_names
        return result</pre>

"""</div><div style="margin-left:40px"><br></div><div style="margin-left:40px">With:</div><div style="margin-left:40px"><br></div><div style="margin-left:40px">"""<br></div><div style="margin-left:80px">def __new__(cls, name, bases, classdict):
</div><div style="margin-left:40px"><pre class="m_7342312465347859571gmail-literal-block">        result = type.__new__(cls, name, bases, classdict)
        result.member_names = classdict.member_names
        return result</pre>

</div><div style="margin-left:40px">"""</div><div style="margin-left:40px"><br></div><div style="margin-left:40px">Removing the incorrect comments & the copying of `classdict`</div><div style="margin-left:40px"><br></div><div style="margin-left:40px">I will go file a bug report to that effect.</div><div><br></div><div>Thanks,</div><div><br></div><div>Joy Diamond.</div>

</div><div dir="ltr"><br><div dir="ltr"><br></div></div><br><div class="gmail_quote"><div dir="ltr">On Sat, Nov 3, 2018 at 7:55 PM Daniel Moisset <<a href="mailto:dfmoisset@gmail.com" target="_blank">dfmoisset@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto">If I understood correctly what you want, it's possible with a metaclass. Check the __prepare__ method at <a href="https://docs.python.org/3/reference/datamodel.html#preparing-the-class-namespace" target="_blank">https://docs.python.org/3/reference/datamodel.html#preparing-the-class-namespace</a> and Pep 3115</div><br><div class="gmail_quote"><div dir="ltr">On Sat, 3 Nov 2018, 20:55 Joy Diamond <<a href="mailto:python.gem@gmail.com" target="_blank">python.gem@gmail.com</a> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div>Team,</div><div><br></div><div>Are we supposed to be able to have our own class dictionary in python 3?</div><div><br></div><div>If we currently cannot -- do we want to be able to?<br></div><div><br></div><div>That we can have out own class dictionary in python 3 is strongly implied in the following at <a href="https://www.python.org/dev/peps/pep-3115/" rel="noreferrer" target="_blank">https://www.python.org/dev/peps/pep-3115/</a> where it says:<br><br>"""</div><div>
<pre class="m_7342312465347859571m_3863327966708087084m_7370047320667964513m_603818574152564677gmail-literal-block">    # The metaclass invocation
    def __new__(cls, name, bases, classdict):
        # Note that we replace the classdict with a regular
        # dict before passing it to the superclass, so that we
        # don't continue to record member names after the class
        # has been created.
        result = type.__new__(cls, name, bases, dict(classdict))
        result.member_names = classdict.member_names
        return result</pre>

"""</div><div><br></div><div>I don't understand this.  As far as I can tell, no matter what class dictionary you pass into `type.__new__` it creates a copy of it.</div><div><br></div>Am I missing something?  Is this supposed to work?  Is the documentation wrong?</div><div dir="ltr"><br></div><div>Thanks,</div><div><br></div><div>Joy Diamond.</div><div><br></div><div>Program that shows that the class dictionary created is not what we pass in --- Shows the actual symbol table is `dict` not `SymbolTable`<br></div><div><br></div><div>class SymbolTable(dict):<br>    pass<br><br>members = SymbolTable(a = 1)<br><br>X = type('X', ((object,)), members)<br><br>members['b'] = 2<br><br>print('X.a: {}'.format(X.a))<br><br>try:<br>    print('X.b: {}'.format(X.b))<br>except AttributeError as e:<br>    print('X.b: does not exist')<br><br>#<br>#   Get the actual symbol table of `X`, bypassing the mapping proxy.<br>#<br>X__symbol_table = __import__('gc').get_referents(X.__dict__)[0]<br><br>print('The type of the actual symbol table of X is: {} with keys: {}'.format(<br>      type(X__symbol_table),<br>      X__symbol_table.keys()))<br></div><div><br></div><div><br></div><div># Prints out</div><div># X.a: 1<br># X.b: does not exist<br># The type of the actual symbol table of X is: <class 'dict'> with keys: dict_keys(['a', '__module__', '__dict__', '__weakref__', '__doc__'])<br></div></div></div></div><div id="m_7342312465347859571m_3863327966708087084m_7370047320667964513m_603818574152564677DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2"><br>
<table style="border-top:1px solid #d3d4de">
        <tr>
        <td style="width:55px;padding-top:13px"><a href="https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=icon" rel="noreferrer" target="_blank"><img src="https://ipmcdn.avast.com/images/icons/icon-envelope-tick-round-orange-animated-no-repeat-v1.gif" alt="" width="46" height="29" style="width:46px;height:29px"></a></td>
                <td style="width:470px;padding-top:12px;color:#41424e;font-size:13px;font-family:Arial,Helvetica,sans-serif;line-height:18px">Virus-free. <a href="https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=link" style="color:#4453ea" rel="noreferrer" target="_blank">www.avast.com</a>
                </td>
        </tr>
</table><a href="#m_7342312465347859571_m_3863327966708087084_m_7370047320667964513_m_603818574152564677_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2" width="1" height="1" rel="noreferrer"></a></div>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" rel="noreferrer" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</blockquote></div>
</blockquote></div>
</blockquote></div>