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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:12:13+00:00 2026-05-23T09:12:13+00:00

If I have a class such as below (only with many more properties), is

  • 0

If I have a class such as below (only with many more properties), is there are clean way to note which fields are required before calling a particular method?

class Example():

    def __init__(self):
        pass

    @property
    """Have to use property methods to have docstrings..."""
    def prop1(self):
        return self._prop1
    @prop1.setter
    def task(self, value):
        # validation logic..
        self._prop1 = value

    def method(self):
        # check all required properties have been added

I could write an array by hand of all required propeties and loop through them in a method, but I was wondering if there is a cleaner way for example by implementing a @requiredProperty descriptor.

The class is used to generate a POST request for a web API. The request has 25+ parameters, some of which are required and some optional.

Rather than on the method calling the request having to loop through an array such as:

required_props = ['prop1','prop2',....]

I was hoping there was a way in Python of adding a required decorator to properties so I wouldn’t have to keep track by hand. E.g.

    @property, @required
    def prop1(self):
        return self._prop1
  • 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-23T09:12:14+00:00Added an answer on May 23, 2026 at 9:12 am

    This should work the same way as in any OO language: A required property must be set during construction time. Calling the objects methods must never leave the object in a “bad” state, so that method can be called on any constructed object.

    If the above doesn’t hold true, you should think about refactoring your code.

    Of course it is always possible to alter a python object to not be valid anymore by poking around in its guts. You don’t do that unless you have a good reason. Don’t bother checking for this, as your program should just blow up in your face whenever you do something stupid so you learn and stop.

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

Sidebar

Related Questions

If I have a simple class such as:- @XmlRootElement public class MyClass { @XmlAttribute(required=true)
i have a class whose equality is based on 2 fields such that if
I have many classes that implements IBuilder<> interface such the ones below UPDATED: each
If I have class names such as left, right, clear and xhtml like <a
In AS3, if I have a class such: public class dude { //default value
I have such class public unsafe class EigenSolver { public double* aPtr {get; private
If I have a base class such that public abstract class XMLSubscription <T extends
I have a class hierarchy as such: +-- VirtualNode | INode --+ +-- SiteNode
I have 3 abstract classes (Customer, Tender, Site). For Customer has inherited class such
I have such a class: public class Cycle { public List<int> Edges { get;

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.