Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Question 3</strong></p> <p>To have <code>cls.__name__</code> be anything you want, (with a nod to delnan's suggestion)</p> <pre><code>def f(clsname): class XYZ: # ... XYZ.__name__ = XYZ # ... return XYZ </code></pre> <p><strong>Question 1</strong></p> <p>The reason that <code>c1 is not c2</code> is that they are two different objects stored at two different locations in memory. </p> <p><strong>Question 4</strong> </p> <p>Try an answer to question 1 and see how it works out for you</p> <p><strong>Question 2</strong></p> <p>It can complicate debugging that their class attributes <code>__name__</code> share a common value and this is bad enough to take pains to avoid. (see question 3). I would maintain though that they don't have the same name. One is named <code>C1</code> and the other is named <code>C2</code> (at least in the scope you are showing. If you were to pass them to a function, then there name in that scope would be the same as the name of parameter that they were passed through)</p> <p>In fact, I'm so sure that they don't have the same name that trying to tell me otherwise is likely to cause me to turn the music up louder and pretend I can't hear you.</p> <p><strong>In response to comment</strong></p> <p>It can be done but it's just wrong. I'll illustrate anyway because it's illuminating:</p> <pre><code>def f(clsname): class XYZ(object): pass XYZ.__name__ = clsname globals()[clsname] = XYZ f('C1') f('C2') print C1 print C2 </code></pre> <p>This just works by sticking the class in the globals dict keyed by <code>clsname</code>. But what's the point? You can stick it in the globals dict under <em>any</em> name in fact because this is just another assignment. You are best off just returning the class from the function and letting the caller decide what name to give the class in it's own scope. You still have the <code>__name__</code> attribute of the class set to the string you pass to the function for debugging purposes.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload