Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1072293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:50:11+00:00 2026-05-16T20:50:11+00:00

I have the following code: def f(): class XYZ: # … cls = type(‘XXX’,

  • 0

I have the following code:

def f():
    class XYZ:
        # ...
    cls = type('XXX', (XYZ, ), {})
    # ...
    return cls

I am now using it as follows:

C1 = f()
C2 = f()

and it seems to work fine: C1 is C2 returns False, there’s no conflict between the class attributes of the two classes, etc.

Question 1

Why is that? How is it possible that C1 and C2 are both shown as class

<'__main__.XXX'>

and yet not the same class?

Question 2

Is there some problem with the fact that I have two identical names for two different classes?

Question 3

I would like to be able to write instead:

f('C1')
f('C2')

with the same effect. Is it possible?

Question 4

If I want C1 to look like a regular class, not main.XXX, is it ok to say:

C1.__name__ = '__main__.C1'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T20:50:11+00:00Added an answer on May 16, 2026 at 8:50 pm

    Question 3

    To have cls.__name__ be anything you want, (with a nod to delnan’s suggestion)

    def f(clsname):
        class XYZ:
            # ...
        XYZ.__name__ = XYZ
        # ...
        return XYZ
    

    Question 1

    The reason that c1 is not c2 is that they are two different objects stored at two different locations in memory.

    Question 4

    Try an answer to question 1 and see how it works out for you

    Question 2

    It can complicate debugging that their class attributes __name__ 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 C1 and the other is named C2 (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)

    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.

    In response to comment

    It can be done but it’s just wrong. I’ll illustrate anyway because it’s illuminating:

    def f(clsname):
        class XYZ(object):
            pass
        XYZ.__name__ = clsname
        globals()[clsname] = XYZ
    
    f('C1')
    f('C2')
    
    print C1
    print C2
    

    This just works by sticking the class in the globals dict keyed by clsname. But what’s the point? You can stick it in the globals dict under any 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 __name__ attribute of the class set to the string you pass to the function for debugging purposes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: def f(cls, value): # cls is a class #
I have the following code: class IncidentTag: def __init__(self,tag): self.tag = tag def equals(self,obj):
I have the following example code: class A(object): def __init__(self, id): self.myid = id
I have the following ruby code: class Mp def initialize Test.new.mytest Work.new.mywork ha address
I have following code in my django application. class Status(object): def __init__(self, id, desc):
I have following code: class EntityBase (object) : __entity__ = None def __init__ (self)
In Python, consider I have the following code: class SuperClass(object): def __init__(self, x): self.x
I have the following code: class MyClass: def __private(self): print Hey man! This is
I have the following IronPython code. class Hello: def __init__(self): pass def add(self, x,
I have the following IronPython code. class Hello: def __init__(self): pass def add(self, x,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.