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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:25:27+00:00 2026-06-09T11:25:27+00:00

I’m creating class which behaves like list(), but you can add strings to it

  • 0

I’m creating class which behaves like list(), but you can add strings to it and these strings will be splitted by spaces on words and these words will be added to the class instance. Same with substraction: substraction removes words from class instance (if any).

I faced with strange behavior in nose tests: behavior changes when I’m simply changing order of assertions.

Here is test:

from nose.tools import *

def test_isub_str(self):
    u = params()

    u += "1 2 3 4"
    u -= "2 3"

    ok_("1" in u, u)
    ok_("2" not in u, u)
    ok_("3" not in u, u)
    ok_("4" in u, u)

This test fails with following output:

Traceback (most recent call last):
  File "tests.py", line 71, in test_isub_str
    ok_("4" in u, u)
    assert expr, msg
AssertionError: 1 4

Strange, the “u” is dumped as “1 4”, but “4” wasn’t found in “u” during assertion testing. And the most interesting happens when I change the order of assertions to following:

ok_("1" in u, u)
ok_("4" in u, u)
ok_("2" not in u, u)
ok_("3" not in u, u)

Then tests are passing OK and this is correct, expected behavior. It looks like “not in” statement changes behavior of following “in” statements. I’m surprized.

Finally, here is my class:

class params():
    def __init__(self):
        self.index = 0 # iterator index
        self.values = [] # list of stored values

    def __iadd__(self, addvalues):
        if isinstance(addvalues, str):
            addvalues = addvalues.split(" ")
        # add all values to this instance if they are list() or params()
        # and not exist in current instance
        if isinstance(addvalues, params) or isinstance(addvalues, list):
            for value in addvalues:
                if isinstance(value, str) and value not in self.values:
                    self.values.append(value)
                else:
                    raise TypeError
            return self
        else:
            raise TypeError

    def __isub__(self, subvalues):
        if isinstance(subvalues, str):
            subvalues = subvalues.split(" ")
        # substract all values from this instance if they are list() or params()
        # and existing in current instance
        if isinstance(subvalues, params) or isinstance(subvalues, list):
            for value in subvalues:
                if isinstance(value, str) and value in self.values:
                    self.values.remove(value)
                else:
                    raise TypeError
            return self
        else:
            raise TypeError

    def __iter__(self):
        return self

    def next(self):
        if self.index >= len(self.values):
            raise StopIteration
        else:
            self.index += 1
            return self.values[self.index - 1]

    def __str__(self):
        return " ".join(self.values)

So question is: why such strange behavior happens? I missed something in iterator functionality? Or this nosetests bug?

  • 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-09T11:25:29+00:00Added an answer on June 9, 2026 at 11:25 am

    No, it’s not a nosetests bug, i think that when you do

    ok_("1" in u, u)
    

    u.index is 1, when you do:

    ok_("2" not in u, u)
    ok_("3" not in u, u)
    

    u.index is 2. And when you do

    ok_("4" in u, u)
    

    u.index is still 2, and your “next” method raises StopIteration because 2 is >= len(self.values).

    In your second case u.index is still 1 when you do:

    ok_("4" in u, u)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
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
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't

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.