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

  • Home
  • SEARCH
  • 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 3344356
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:03:36+00:00 2026-05-18T01:03:36+00:00

I have a set of classes which can contain an optional name attribute. The

  • 0

I have a set of classes which can contain an optional name attribute. The reason is that the classes will get a default name if the attribute is set, but individual classes can still have a custom name if needed.

What I want is to be able to get the name attribute from both the class (without any class instance) and from instances of a class.

class NameMixin(object):
    def _get_name(self):
        if getattr(self, '_name', ''):
            return self._name
        else:
            return self.__class__.__name__

    def _set_name(self, name):
        self._name = name

    name = property(_get_name, _set_name)

class A(NameMixin):
    name = 'Class A'

class B(NameMixin):
    pass

Here, the class A customizes the name, while class B does not.

>>> a = A()
>>> a.name
'Class A'
>>> A.name
'Class A'

As seen, this works as it should

>>> b = B()
>>> b.name
'B'
>>> B.name
<property object at 0x7fd50a38c578>

This does not work as I want! Getting the name from a specific instance works as it should, but attempting to get the name from the class returns a property object.

Is it possible to get the name directly from a class without jumping through hoops with the property object (which I really can’t check for in the place where I need the class-attribute anyway.)

  • 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-18T01:03:36+00:00Added an answer on May 18, 2026 at 1:03 am
    class NameMixinMeta(type):
        def _get_name(self):
            return getattr(self, '_name', self.__name__)
    
        def _set_name(self, name):
            self._name = name
    
        name = property(_get_name, _set_name)
    
    class NameMixin(object):
        __metaclass__ = NameMixinMeta
        def _get_name(self):
            return getattr(self, '_name', self.__class__.__name__)
    
        def _set_name(self, name):
            self._name = name
    
        name = property(_get_name, _set_name)
    
    class A(NameMixin):
        _name = 'Class A'
    
    class B(NameMixin):
        pass
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have data model classes that contain private fields which are meant to be
I have a set of some classes which are all capable of being constructored
I have a set of XSDs from which I generate data access classes, stored
If have a set of classes that all implement an interface. interface IMyinterface<T> {
I have a set of callback classes that I use for handling callbacks with
I have a set of core, complicated JavaScript data structures/classes that I'd like to
I have been assigned a project to develop a set of classes that act
Model: I have a model in which one Installation can contain multiple Computer Systems.
I have a set of classes ( MyClass1, MyClass2, MyClass3 ) that implement an
I would love to be able to have a common set of Classes in

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.