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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:13:31+00:00 2026-06-18T06:13:31+00:00

Consider the following code: class BaseClass(object): def _del_property(attr): Abstract deller def del_attr(self): setattr(self, attr,

  • 0

Consider the following code:

class BaseClass(object):
    def _del_property(attr):
        """Abstract deller"""
        def del_attr(self):
            setattr(self, attr, None)
        return del_attr

    def _set_property(attr):
        """Abstract setter."""
        def set_attr(self, x):
            setattr(self, attr, x)            
        return set_attr

    def _get_property(attr):
        """Abstract getter"""
        def get_attr(self):
            getattr(self, attr)
        return get_attr

    _name = None
    name = property(fget=_get_property('_name'), fset=_set_property('_name'))


class Component(BaseClass):

    _material = None
    material = property(fget=_get_property('_material'), fset=_set_property('_material'), fdel=_del_property('_material'))

How come _get_property, _set_property and _del_property are not inherited?
How can it be achieved?

It should work for derived classes in the same source file, as well as for derived classes in a separate file, which import this source file with a

from filename import *
  • 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-06-18T06:13:32+00:00Added an answer on June 18, 2026 at 6:13 am

    When creating a class, a new namespace is created. Python reads the block of code inside your class block and then passes the information to type (or the corresponding metaclass). In this case, python complains because when setting up the Component class, _get_property isn’t defined within that scope — perhaps BaseClass._get_property, but that probably won’t work like you want it to either.

    One easy fix is to make those functions module level functions since the class namespace has access to the module namespace:

    def _del_property(attr):
        """Abstract deller"""
        def del_attr(self):
            setattr(self, attr, None)
        return del_attr
    
    def _set_property(attr):
        """Abstract setter."""
        def set_attr(self, x):
            setattr(self, attr, x)            
        return set_attr
    
    def _get_property(attr):
        """Abstract getter"""
        def get_attr(self):
            getattr(self, attr)
        return get_attr
    
    
    class BaseClass(object):
    
        _name = None
        name = property(fget=_get_property('_name'), fset=_set_property('_name'))
    
    
    class Component(BaseClass):
    
        _material = None
        material = property(fget=_get_property('_material'), fset=_set_property('_material'), fdel=_del_property('_material'))
    

    Perhaps it’s more instructive to look at the class while we’re creating it:

    class BaseClass(object):
    
        def _del_property(attr):
            """Abstract deller"""
            def del_attr(self):
                setattr(self, attr, None)
            return del_attr
    
        print type(_del_property)
    print type(BaseClass._del_property)
    

    This will print:

    <type 'function'>
    <type 'instancemethod'>
    

    So, you’re dealing with a regular function inside BaseClass, but it becomes an instancemethod after type does it’s magic.


    Personally, I would just create a convenience function:

    def property_api(name,fget=True,fset=True,fdel=True):
        return property(fget=_get_property(name) if fget else None,
                        fset=_set_property(name) if fset else None,
                        fdel=_del_property(name) if fdel else None)
    

    Which you can then use. If you really want to, you could even put it on BaseClass as a staticmethod:

     class BaseClass(object):
          property_api = staticmethod(property_api)
    

    Then your derived classes would look like:

     class DerivedClass(BaseClass):
          _material = None
          material = BaseClass.property_api('_material')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

consider the following python code class Base(object): def __init__(self): self.foo = 5 class Derived(Base):
consider the following code: class MyClass(object): def __init__(self): self.data_a = np.array(range(100)) self.data_b = np.array(range(100,200))
Consider the following code in Python: class A(object): CLASS_ATTRIBUTE = 42 def f(self): return
Consider the following code: class A(object): def do(self): print self.z class B(A): def __init__(self,
Consider the following code: class Base(object): @classmethod def do(cls, a): print cls, a class
Consider the following code: // ... public class BaseClass { public BaseClass (int theParam)
Consider the following code: abstract class Foo<T> where T : Foo<T>, new() { void
Consider the following code Class.prototype.init = function() { var self = this; var onComplete
Consider the following code: class A { B* b; // an A object owns
Consider the following code: abstract class SomeClassX<T> { // blah } class SomeClassY: SomeClassX<int>

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.