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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:32:50+00:00 2026-06-11T00:32:50+00:00

I found some unexpected behavior when debugging my application. Does anyone know why I

  • 0

I found some unexpected behavior when debugging my application. Does anyone know why I get the results described below?

from google.appengine.ext import ndb

class Person(ndb.Model):
  name = ndb.StringProperty()
  shared = ndb.BooleanProperty(default=False)

class Department(ndb.Model):
  name = ndb.StringProperty()
  persons = ndb.KeyProperty(kind=Person, repeated=True)

  @property
  def all_department_resources(self):    
    emp_list = self.persons
    for p in Person.query().filter(Person.shared == True):
      emp_list.append(p.key)
    return emp_list 

p1 = Person(name='Jane').put()
p2 = Person(name='Siri').put()
p3 = Person(name='Joe', shared=True).put()
Department(name='Finance',persons=[p1,p2]).put()

print '\n*First run*'
for d in Department.query():
  print '>>' + d.name
  for p in d.all_department_resources:   
    print p.get().name

print '\n*Second run:*'
for d in Department.query():
  print '>>' + d.name
  for p in d.all_department_resources:   
    print p.get().name

Output:

*First run*
>>Finance
Jane
Siri
Joe

*Second run:*
>>Finance
Jane
Siri
Joe
Joe

My question is simple: Where did the second Joe come from?

  • 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-11T00:32:51+00:00Added an answer on June 11, 2026 at 12:32 am

    In all_department_resources you add Joe to the self.persons list. And on the second call you add it again. You can confirm this behavior by executing your text block a third time and find three Joes….

    Why?

    emp_list = self.persons
    

    does not make a copy of the list, but references it. Everything you do to emp_list happens in self.persons too.

    What can I do about it?
    Either make a copy with

    emp_list = list(self.persons)
    

    Or you can just add the Query to the return statement:

      @property
      def all_department_resources(self):    
        return self.persons + Person.query().filter(Person.shared == True)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found some information about this on Scott Hanselmans Blog Does anybody exactly know
I observed some unexpected behavior with TTLauncherView from Three20. After creating a standard view
I found some classes designed for debugging in package com.sun.jdi like VirtualMachine , but
I found some really great code from Matt Gallagher for use with making Undo
I just read about Statement Expressions Extension in GCC, and I found some unexpected
During investigation of some problem I found that the reason was unexpected different conversion
I am trying to use 'py2app' to generate a standalone application from some Python
Today I met some unexpected behavior on doctrine (1.2). Situation I've a Document class,
I'm seeing some unexpected behavior in Grails' createCriteria. I have a domain class that
Okay, so I'm observing some behavior that I would certainly call 'unexpected' and I'm

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.