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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:55:58+00:00 2026-06-10T21:55:58+00:00

I have created a word object, which consists of just two methods, and takes

  • 0

I have created a word object, which consists of just two methods, and takes just two parameters. In spite of this apparent simplicity it is behaving in a way that’s beyond my comprehension: if I create two instances of the same object, with the same first argument (“dissembling” in this case) the second instance somehow interferes with the first. Printing the instances reveals that they are indeed separate, so why are the interacting in this way?

# Example tested with Python 2.7.3

from collections import namedtuple
DefinitionTuple = namedtuple("Definition", "word word_id text pos")


class Word(object):

    def __init__(self, word, defs=None):
        """"""
        self.definitions = []       
        self.word = word

        if defs != None:
            for each in defs:
                try:
                    each.pos
                    if each.word.lower() == self.word.lower():
                        self.definitions.append(each)
                except AttributeError:
                    raise AttributeError("Definitions must be named tuples")

            self.orderDefinitions()


    def orderDefinitions(self):
        """"""  
        ordered = sorted(self.definitions, key=lambda definition: definition.pos)
        for i,each in enumerate(ordered):
            each.pos = (i+1)

        self.definitions = ordered


class Definition(object):
    """"""
    def __init__(self, definition):
        """Incoming arg is a single namedtuple"""       
        self.word = definition.word
        self.word_id = definition.word_id
        self.text = definition.text
        self.pos = definition.pos       


if __name__ == "__main__":
    nt1 = DefinitionTuple("dissemble", 5, "text_string_a", 1)
    nt2 = DefinitionTuple("dissemble", 5, "text_string_b)", 2)
    nt3 = DefinitionTuple("dissemble", 5, "text_string_c", 3)   

    # Definiton objects
    def_1 = Definition(nt1)
    def_2 = Definition(nt2)
    def_3 = Definition(nt3)



    dissemble = Word("dissemble", [def_1, def_2, def_3])
    print "first printing: "
    for each in dissemble.definitions:
        print each.pos, each.text

    # create a new instance of Word ...
    a_separate_instance = Word("dissemble", [def_3])    

    # ... and now the 'pos' ordering of my first instance is messed up!
    print "\nnow note how numbers differ compared with first printing:"     
    for each in dissemble.definitions:
        print each.pos, each.text
  • 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-10T21:56:00+00:00Added an answer on June 10, 2026 at 9:56 pm

    You create a new instance of Word, but you reuse the same instance of def_3:

    a_separate_instance = Word("dissemble", [def_3])    
    

    which is stateful. If we look inside using vars:

    print vars(def_3)
    # create a new instance of Word ...
    a_separate_instance = Word("dissemble", [def_3])    
    print vars(def_3)
    

    We see

    {'text': 'text_string_c', 'word': 'dissemble', 'pos': 3, 'word_id': 5}
    {'text': 'text_string_c', 'word': 'dissemble', 'pos': 1, 'word_id': 5}
    

    due to orderDefinitions.

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

Sidebar

Related Questions

I have created this function to check abecedarian with while loop (A word is
I have basic hello word example of Prime Faces. I have created dynamic web
Im using c# .net windows form application. I have created a database which has
I have some C# automation code which does interesting things with Microsoft.Office.Interop.Word.Document objects. The
I have created small test web application which makes use of LINQ to SQL.
I have a Word document with two embedded Excel files (added using Insert ->
I am working on an iPhone application in which I have HTML pages created
I have created a Word template that I am then processing via the OpenXML
I have a Word document that contains about 4000 form fields which I have
I have no firm idea how to word this question, so forgive me if

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.