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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:25:06+00:00 2026-05-24T00:25:06+00:00

I have a Python object which has certain attributes that are set after the

  • 0

I have a Python object which has certain attributes that are set after the constructor is called. For example,

def Student(object):
    def __init__(name, address=None):
        self.name = name
        self.address = address

stud = Student("John")
stud.address = "123 Main St. New York, NY"

I would like to be able to have a function be called when the address attribute is set which will do things such as reformat the address or do a lookup and add in the zip code, etc. Is there a way to accomplish this within the object’s definition or should I just have to do that myself every time I set the address attribute?

  • 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-24T00:25:07+00:00Added an answer on May 24, 2026 at 12:25 am

    What you probably need is a property concept.

    class C(object):
      def __init__(self, x):
        self._x = x
    
      def get_x(self):
        return self._x
      def set_x(self, x):
        self._x = x
      x = property(get_x, set_x)
    
    obj = C(5)
    obj.x = 6    # set
    print obj.x  # get
    

    See this link for more details: http://snippets.dzone.com/posts/show/954

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

Sidebar

Related Questions

I have a python module that defines a number of classes: class A(object): def
I have created some python code which creates an object in a loop, and
I want a Python object that will monitor whether other objects have changed since
I have an object that is basically a Python implementation of an Oracle sequence.
I have Karmic Koala which has Python 2.6 installed by default. However I can't
I have some python module, which has a class ModuleClass and I can't modify
I have an object gui_project which has an attribute .namespace , which is a
I have a JSON object that I am trying to read using Python but
I have a class that I'm testing which has as a dependency another class
For example, suppose I have the code: class Foo(object): def bar(self, x): '''blah blah

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.