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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:03:39+00:00 2026-05-26T11:03:39+00:00

I’m new to python and I would like some help. I created some classes

  • 0

I’m new to python and I would like some help. I created some classes with properties in order to keep me from passing meaningless arguments.

So for example I have this class

class match(object):
     __teams=(None,None)

     def setTeams(self,tms):
          if type(tms) != type(list()) and type(tms) != type(tuple()):
               raise Exception("Teams must be a list of length 2")
          if len(tms) != 2:
               raise Exception("Teams must be a list of length 2")
          if (type(tms[0])==type(str()) or (type(tms[0])==type(unicode()))) \
          and (type(tms[1])==type(str()) or type(tms[1])==type(unicode())):
               self.__teams=tms
          else:
               raise Exception("Both teams must be strings")
          return

      teams=property(getTeams,setTeams)

If I write

match1=match()
match1.teams=(2,4)

I get an exception as I should, but

match1.teams[0]=5

does not raise an exception and passes the number 5. Please keep in mind that this is not all of the class, I just wrote down only what is relative to my question, so assume that the code behaves as I describe.

I guess this is because everything is passed by reference in python but I have to be careful not to assign meaningless data to my objects which defeats the purpose of having properties in the first place.

So, is there a way to fix that apart from not using lists or do I have to learn to live with it?

  • 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-26T11:03:40+00:00Added an answer on May 26, 2026 at 11:03 am

    One of the advantages of propertys is being able to do data validation — sometimes it is really important to make sure you get something very specific.

    In your case you need to do one of two things:

    • store your teams data in a structure that can’t be modified, such as a tuple or namedtuple; then when the data is retrieved it cannot be changed

    or

    • have your get method return a copy of the data, so any modification do not mess up your original

    The first solution (immutable types) looks like this:

    class match(object):
        __teams=(None,None)
    
        def setTeams(self,tms):
            "any sequence type will do, as long as length is two"
            if len(tms) != 2:
                raise TypeError(
                    "Teams must be a sequence of length 2"
                    )
            if not isinstance(tms[0], (str, unicode)):
                raise TypeError(
                    "Team names must be str or unicode, not %r" % type(tms[0])
                    )
            if not isinstance(tms[1], (str, unicode)):
                raise TypeError(
                    "Team names must be str or unicode, not %r" % type(tms[0])
                    )
            self.__teams = tuple(tms)
    
        def getTeams(self):
            return self.__teams
    
        teams=property(getTeams,setTeams)
    

    And when you try to assign after getting the value, this happens:

    Traceback (most recent call last):
      File "test.py", line 22, in <module>
        match1.teams[0]=5
    TypeError: 'tuple' object does not support item assignment
    

    The second solution (returning a copy instead of the original) looks like this:

    class match(object):
        __teams=(None,None)
    
        def setTeams(self,tms):
            "any sequence type will do, as long as length is two"
            if len(tms) != 2:
                raise TypeError(
                    "Teams must be a sequence of length 2"
                    )
            if not isinstance(tms[0], (str, unicode)):
                raise TypeError(
                    "Team names must be str or unicode, not %r" % type(tms[0])
                     )
            if not isinstance(tms[1], (str, unicode)):
                raise TypeError(
                    "Team names must be str or unicode, not %r" % type(tms[0])
                    )
            self.__teams = list(tms)
    
        def getTeams(self):
            return list(self.__teams)
    
        teams=property(getTeams,setTeams)
    
    # and the code in action...
    match1=match()
    match1.teams=('us',u'them')
    
    match1.teams[0]=5
    print match1.teams
    

    which has the following results:

    ['us', u'them']
    

    As you can see, the changes did not make it back into the match object.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have some data like this: 1 2 3 4 5 9 2 6
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from

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.