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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:39:29+00:00 2026-05-30T14:39:29+00:00

I’m creating custom class based views for a Django project and faced a design

  • 0

I’m creating custom class based views for a Django project and faced a design decision concerning attributes.

Getter functions

I looked at Django’s generic views, and saw that there are quite a lot of classes that provide both class variables as well as custom getter functions. e.g.

class FormMixin(object):
    initial = {}
    def get_initial(self):
        return self.initial

This design decision makes sense, because that way, you can call super() in an overriding method of an extending class.

Properties

In my project, there are certain values that I could sometimes override using a simple value, but that sometimes have to be generated dnyamically. At first I created methods similar to the above, but then thought that maybe properties are a nicer way to do this.

class MyBaseView(OtherView):
    counter = None

class MyExtendingView(MyBaseView):
    counter = 5

class MyOtherExtendingView(MyBaseView):
    @property
    def counter(self):
        return self.other_function()

instance.__dict__

I plan on using those values inside a template. I pass instances of the extending views to a template and display the attributes in the following way:

context['class_instances'] = [MyExtendingView(), MyOtherExtendingView()]

{% for instance in class_instances %}
    {{ instance.counter|default:'' }}
{% endfor %}

Now as this is a template, and it doesn’t really matter whether the instance attribute is a value or a function, I could also write my classes like this:

class MyExtendingView(MyBaseView):
    counter = 5

class MyOtherExtendingView(MyBaseView):
    def counter(self):
        return self.other_function()

The def counter will replace the old value-based attribute in instance.__dict__.

Question

To get to the question, which of the listed approaches is the best choice?

  • The getter approach is nice, as it allows super()-calling the parent’s getter function, but in most of my cases that will probably never be needed.
  • The property approach is much simpler and causes less code to be written. I can’t super()-call a parent’s property function though. I won’t be needing this behavior in most cases, but there could be cases where I need super() functionality, so there would be a mix of simple attributes that can be overridden using properties and custom getter functions.
  • The last approach is even less code than the property approach, but I doubt it’s good style.
  • 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-30T14:39:30+00:00Added an answer on May 30, 2026 at 2:39 pm

    The property approach is much simpler and causes less code to be written. I can’t super()-call a parent’s property function though.

    You actually can access parent properties via super():

    class A(object):
        @property
        def prop(self):
            return 'A.prop'
    
    class B(A):
        @property
        def prop(self):
            return '%s - %s' % (super(B, self).prop, 'B.prop')
    
    >>> b = B()
    >>> b.prop
    'A.prop - B.prop'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which 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.