1

trying to build a very simple Python for loop I don't know, where a strange behavior results from.

This is the code:

def __init__(self, create_test_data):
    if(create_test_data):
        self.sites = []
        for x in range(2):
            site = Site(
                title='article' + str(x),
                alias='alias' + str(x),
                keywords='keyword' + str(x),
                description='description' + str(1),
                author='Martin Schophaus'
                )
            for y in range(2):
                article = Article()
                for z in range(2):
                    content = Content(
                        '<div>Test content {}-{}-{}</div>'.format(
                            str(x), str(y), str(z)
                        ))
                    article.content.append(content)
                site.articles.append(article)
            self.sites.append(site)

At the end, the variable self.sites should contain all sites and a site should contain the sites articles and an article should contain the articles content. But currently the result is: The sites contains every content/articles from all sites. I'm very confused, because it doesn't work.

Coming from C# this is not what I expected. Thanks for every help :)

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

0 Answers0