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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:01:13+00:00 2026-05-25T06:01:13+00:00

Possible Duplicate: Can I get a reference to the 'owner' class during the init

  • 0

Possible Duplicate:
Can I get a reference to the 'owner' class during the init method of a descriptor?

Code is worth a thousand words:

>>> class ShortRib(object):
>>>     def __init__(self, owner):
>>>         self.owner = owner
>>> 
>>>     ... some more methods and stuff ...
>>> 
>>> 
>>> class Cow(object):
>>>     shortRib = ShortRib(self)
>>> 
>>> 
>>> class BrownCow(Cow):
>>>     pass
>>> 
>>> BrownCow.shortRib.owner
<class '__main__.BrownCow'>

This doesn’t work, though i wish it would. Basically, I want each class to have some static/class variables (i’m not sure which it is in this case?) but need each of those guys to know who (which class) it belongs to. Unfortunately, I can’t “get” at the class in the body of the class declaration. Of course, I could always do this using a decorator:

>>> def vars(**kwargs):
>>>     def wrap(cls):    
>>>         for k, w in kwargs.items():
>>>             setattr(cls, k, w(cls))
>>>         return cls
>>>     return wrap
>>> 
>>> @vars(shortRib=lambda cls: ShortRib(cls)
>>> class BrownCow(Cow):
>>>     ...
>>>
>>> BrownCow.shortRib.owner

which would work. Another way would to have a class decorator that goes through all the shortRibs and similar static variables and sets their owner after the class declaration is complete. However, this seems like an incredibly roundabout and unintuitive way of doing what should be a pretty simple operation: having the static/class members of a class know who they belong to.

Is there a “proper” way of doing this?

Clarification:

I want these members to belong to the class, not to the instances. I’m trying to go for a almost-purely-functional style, using classes only for inheritance of shared behavior, and not creating instances of them at all. Instances would tend to give my functions access to arbitrary instance data shared across all functions, which would break the pure-functioness I am trying for. I could just use empty instances which I don’t touch, but I think using pure classes would be cleaner.

  • 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-25T06:01:14+00:00Added an answer on May 25, 2026 at 6:01 am

    You can easily do this in __new__:

    class ShortRib(object):
        def __init__(self, owner):
            self.owner = owner
    
    class Cow(object):
        shortRib = None
        def __new__(cls, *args, **kwargs):
            if cls.shortRib == None:
                cls.shortRib = ShortRib(cls)
            return super(Cow, cls).__new__(cls, *args, **kwargs)
    
    Cow()
    Cow.shortRib.owner
    

    Or even __init__, if you don’t mind referencing self.__class___.

    You can also do it with a metaclass:

    class ShortRib(object):
        def __init__(self, owner):
            self.owner = owner
    
    class MetaCow(type):
        def __new__(cls, name, base, attrs):
            attrs['shortRib'] = ShortRib(cls)
            return super(MetaCow, cls).__new__(cls, name, base, attrs)
    
    class Cow(object):
        __metaclass__ = MetaCow
    
    Cow.shortRib.owner
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Returning a reference can work? Why this code works ( matrix is
Possible Duplicate: Dynamically retrieving current method's name Obj-C introspection: How can a method reference
Possible Duplicate: How can I get a list of all the defined variables in
Possible Duplicate: How can I find the method that called the current method? I
Possible Duplicate: Can I create an automatic property (no private member) with get and
Possible Duplicate: Why can templates only be implemented in the header file? Undefined reference
Possible Duplicate: How can I get the client's IP address in ASP.Net MVC? I
Possible Duplicate: How can I find the method that called the current method? How
Possible Duplicates: Get method name and type using lambda expression Can I use Expression<Func<T,
Possible Duplicate: Can I get the names of all the domains a Linux computer

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.