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 3609206
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:35:51+00:00 2026-05-18T21:35:51+00:00

I have: class C: aaa=2 class B: def __init__ (self,name): self.name self.value How can

  • 0

I have:

class C:
    aaa=2


class B:
     def __init__ (self,name):
         self.name
         self.value

How can i define class C so when i dynamically set attribute to instance it make that attribute instance of class B. And attribute name of class B have to have attribute name equal string of name of that new attribute in class C and attribute value of B instance have to have value what set in new attribute in instance of class C.

Have to give me that result:

>> c=C()
>> c.whatever= 'strinstrinsstring'
>> isinstance(c.whatever,B)
True
>> c.whatever.value
'strinstrinsstring'
>>c.whatever.name
'whatever'
  • 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-18T21:35:52+00:00Added an answer on May 18, 2026 at 9:35 pm

    Just smartly override __setattr__. If you want to do it only for a specific attribute, then put in a special case for the attribute name that you want to look for:

    >>> class B:
        def __init__(self, name, value):
            self.name = name
            self.value = value
    
    
    >>> class C:
        def __setattr__(self, name, value):
            if name == 'makeMeB':
                newb = B(name, value)
                self.__dict__[name] = newb
            else:
                self.__dict__[name] = value
    
    >>> c = C()
    >>> c.makeMeB = 'foo'
    >>> isinstance(c.makeMeB, B)
    True
    >>> c.makeMeB.name
    'makeMeB'
    >>> c.makeMeB.value
    'foo'
    >>> c.blah = 'foo'
    >>> isinstance(c.blah, B)
    False
    

    If you want it for every attribute, just forget the if and it’ll do it for everything:

    >>> class B:
        def __init__(self, name, value):
            self.name = name
            self.value = value
    
    
    >>> class C:
        def __setattr__(self, name, value):
            attr_as_b = B(name, value)
            self.__dict__[name] = attr_as_b
    
    >>> c = C()
    >>> c.makeMeB = 'foo'
    >>> isinstance(c.makeMeB, B)
    True
    >>> c.makeMeB.name
    'makeMeB'
    >>> c.makeMeB.value
    'foo'
    >>> c.amIalsoB = 'well?'
    >>> isinstance(c.amIalsoB, B)
    True
    >>> c.amIalsoB.name
    'amIalsoB'
    >>> c.amIalsoB.value
    'well?'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this class in Scala: object Util { class Tapper[A](tapMe: A) { def
I have the following program. module C def self.included(base) base.extend(ClassMethods) end module ClassMethods def
Here is my custom class that I have that represents a triangle. I'm trying
I have following situation (simplified, of course): MyDomain.groovy: class MyDomain { MyAnotherDomain anotherDomain //
I have a little problem. Class ll: interface jj{ public class ll implements gg{
Having something like this this : public abstract class AAA { protected abstract virtual
I have a class containing Enum with values. (names) In other class I would
I have a class: class A: s = 'some string' b = <SOME OTHER
I'm running into some type of a scope issue that's preventing instance variables from
My a.csv is: 001,哈哈大学 002,拉拉大学 003,啊啊啊大学 004,文网文大学 005,卡卡卡大学 006,请求权大学 007,凤飞飞大学 And my str_loader.py is:

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.