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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:21:21+00:00 2026-06-16T21:21:21+00:00

I would like to declare properties in the constructor of my class. The class

  • 0

I would like to declare properties in the constructor of my class.

The class MaterialOne shows how I have it currently. Every property has to be defined separately. However, I will have groups of similar properties, which I would like to give the same fset/fget/fdel.

Since it requires a lot of code to write all the properties explicit, I would like to define the properties in a more concise way. I therefore thought of letting the constructor handle this. Class MateralTwo shows how I have that in mind.

Unfortunately it doesn’t work, as I get TypeErrors:

TypeError: get_property() takes exactly 1 argument (2 given)

I can understand why it complains, but I can’t think of any solution. I don’t necessarily want to define the properties from a list in the constructor. What I am looking for is a more concise and clean method of defining them.

class MaterialOne(object):
    def __init__(self):
        pass;

    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):
            if getattr(self, attr) is not None:
                return getattr(self, attr); 
            else:
                return 'Some calculated value..'
        return get_attr

    _young = None;
    _shear = None;
    _poisson = None;

    young = property(fget=get_property('_young'), fset=set_property('_young'), fdel=del_property('_young'));
    shear = property(fget=get_property('_shear'), fset=set_property('_shear'), fdel=del_property('_shear'));
    poisson = property(fget=get_property('_poisson'), fset=set_property('_poisson'), fdel=del_property('_poisson'));


class MaterialTwo(object):
    def __init__(self):
        properties = ['young', 'shear', 'poisson'];
        self.create_properties(properties)

    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):
            if getattr(self, attr) is not None:
                return getattr(self, attr); 
            else:
                return 'Some calculated value..'
        return get_attr        

    def create_properties(self, items):
        for item in items:
            storage = '_'+item;
            setattr(self, storage, None);
            setattr(self, item, property(fget=self.get_property(storage), fset=self.set_property(storage), fdel=self.del_property(storage)));


steel = MaterialOne(); 
steel.young = 2e11;
steel.poisson = 0.3;
print steel.poisson
print steel.shear

carbon = MaterialTwo();
carbon.young = 2e11;
carbon.poisson = 0.3;
print carbon.poisson
print carbon.shear

To clarify some more on the code. What I would like to write are Classes for materials, Solid, Liquid, Gas, each of them a subclass of Material. Many material properties will just be assigned. Some can be calculated based on which have been defined. Given two elastic moduli a third can be calculated for instance.

This I have implemented now using something quite similar as MaterialOne. However, as I am getting more material properties, and will also include more of these kind of calculations, I would like to make it cleaner, more organized. Writing it as I did in MaterialTwo is a possibility to me.

  • 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-16T21:21:22+00:00Added an answer on June 16, 2026 at 9:21 pm
    setattr(self, item, property(...
    

    This is ultimately your problem. Since properties are descriptors, they must be bound to the class, not the instance. You will need to override __getattr__(), __setattr__(), and __delattr__() and implement it there.

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

Sidebar

Related Questions

Here is a generic class that I have defined, what I would like to
I would like to know how do I declare a record, that has some
I have a UserControl with a few boolean properties in it. I would like
I have an interface with properties. I would like to know the way to
I'm currently looking at creating an abstract class Building . I would then like
I would like to have a 'relationship' in an inherited (mixin) class. However, when
I would like to declare some integer constants in PowerShell. Is there any good
i would like to do this: DECLARE @TmpTable TABLE = select * from someTable
I would declare an empty String variable like this: string myString = string.Empty; Is
I have declared an enum in my server code, and would like my client

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.