Re: Re: Django python fuction
Thanks 2014年6月11日水曜日 10時37分43秒 UTC+9 Qiancong: > > > Yeah, I think hito koto need a function like copy.deepcopy.. I think he > known copy.deepcopy before(as his example said), just not known how to > write a funtion work as copy.deecopy does for list. > > -- > moqia...@gmail.com > > *From:* François Schiettecatte > *Date:* 2014-06-11 00:21 > *To:* django-users > *Subject:* Re: Django python fuction > Wouldn't the deep copy module handle this for you: > > https://docs.python.org/2/library/copy.html > > François > > On Jun 10, 2014, at 12:17 PM, hito koto > > wrote: > > > Hi, Qiancong : > > > > I was looking for exactly this、 > > Thank you ver much! > > > > > > 2014年6月10日火曜日 23時35分52秒 UTC+9 Qiancong: > > > > Hi, hito koto: > > I think you want to deep copy the list. The following code maybe > helpful: > > def dcopy(obj): > > if not isinstance(obj, list): > > return obj > > return [dcopy(x) for x in obj] > > > > But this function only deep copy for list. You can change code as what > I did for dic, set, tuple , etc .. > > moqia...@gmail.com > > > > From: François Schiettecatte > > Date: 2014-06-10 22:07 > > To: django-users > > Subject: Re: Django python fuction > > You need to use .append() to add elements to a list, I suggest you take > a look at the python tutorial: > > > > https://docs.python.org/2/tutorial/ > > > > Not quite sure what you are doing with i or elem either. > > > > François > > > > On Jun 10, 2014, at 10:01 AM, hito koto wrote: > > > > > So, I also have errors: > > > > > > > > > >>> def foo(x): > > > ... myList = [] > > > ... if isinstance(x, list): > > > ... for i in x: > > > ... elem = i > > > ... return myList + foo(elem) > > > ... else: > > > ... return 1 > > > ... > > > >>> > > > >>> foo(a) > > > Traceback (most recent call last): > > > File "", line 1, in > > > File "", line 6, in foo > > > File "", line 6, in foo > > > TypeError: can only concatenate list (not "int") to list > > > > > > > > > > > > > > > > > > 2014年6月10日火曜日 22時53分28秒 UTC+9 François Schiettecatte: > > > You are redefining 'list' on line 2, rename list to myList as follows: > > > > > > def foo(x): > > > myList = [] > > > if isinstance(x, list): > > > for i in x: > > > elem = i > > > return myList + foo(i) > > > else: > > > return 1 > > > > > > And take this to a python list, this is for django. > > > > > > Cheers > > > > > > François > > > > > > > > > On Jun 10, 2014, at 9:43 AM, hito koto wrote: > > > > > > > hi, > > > > > > > > I have this erroes: > > > > > > > > >>> def foo(x): > > > > ... list = [] > > > > ... if isinstance(x, list): > > > > ... for i in x: > > > > ... elem = i > > > > ... return list + foo(i) > > > > ... else: > > > > ... return 1 > > > > ... > > > > >>> foo(a) > > > > Traceback (most recent call last): > > > > File "", line 1, in > > > > File "", line 3, in foo > > > > TypeError: isinstance() arg 2 must be a class, type, or tuple of > classes and types > > > > > > > > > > > > > > > > 2014年6月10日火曜日 22時27分42秒 UTC+9 Andrew Farrell: > > > > In general, I recommend adding the line "import pdb;pdb.set_trace()" > > > > to the top of your function and walking through it to see why it > doesn't work. > > > > > > > > def foo(x): > > > > import pdb;pdb.set_trace() > > > > > > > > list = [] > > > > if isinstance(x, list): > > > > for i in x: > > > > elem = i > > > > return list + foo(i) > > > > else: > > > > return 1 > > > > > > > > See https://docs.python.org/2/library/pdb.html on how pdb works. > > > > There are faster ways to debug, but when starting out, pdb lets you > see what is happening as you run the function. > > > > > > > > > > > > Some questions I have about this function: > > > > - What is the purpose of the "elem" function? It is never accessed. > > > > - What is the purpose of returning 1 if the argument is not a list? > > > > - Why is it named "foo" rather than something that tells me what the > purpose of the function is? > > > > > > > > > > > > On Tue, Jun 10, 2014 at 8:16 AM, hito koto > wrote: > > > > Hello, > > > > > > > > I don't know how can i do to change to write python function > > > > I want to following code change to write python function or change > to write recursive definition > > > > >>> y = [10, 12, [13, [14, 9], 16], 7] > > > > >>> z = copy.deepcopy(y) > > > > >>> y > > > > [10, 12, [13, [14, 9], 16], 7] > > > > >>> z > > > > [10, 12, [13, [14, 9], 16], 7] > > > > >>> z[2][1][1] > > > > 9 > > > > >>> z[2][1][1] = 88 > > > > >>> z > > > > [10, 12, [13, [14, 88], 16], 7] > > > > [10, 12, [13, [14, 9], 16], 7] > > > > >>> > > > > > > > > this is my use function but n
Re: Re: Django python fuction
Yeah, I think hito koto need a function like copy.deepcopy.. I think he known copy.deepcopy before(as his example said), just not known how to write a funtion work as copy.deecopy does for list. moqianc...@gmail.com From: François Schiettecatte Date: 2014-06-11 00:21 To: django-users Subject: Re: Django python fuction Wouldn't the deep copy module handle this for you: https://docs.python.org/2/library/copy.html François On Jun 10, 2014, at 12:17 PM, hito koto wrote: > Hi, Qiancong : > > I was looking for exactly this、 > Thank you ver much! > > > 2014年6月10日火曜日 23時35分52秒 UTC+9 Qiancong: > > Hi, hito koto: > I think you want to deep copy the list. The following code maybe helpful: > def dcopy(obj): > if not isinstance(obj, list): > return obj > return [dcopy(x) for x in obj] > > But this function only deep copy for list. You can change code as what I did > for dic, set, tuple , etc .. > moqia...@gmail.com > > From: François Schiettecatte > Date: 2014-06-10 22:07 > To: django-users > Subject: Re: Django python fuction > You need to use .append() to add elements to a list, I suggest you take a > look at the python tutorial: > > https://docs.python.org/2/tutorial/ > > Not quite sure what you are doing with i or elem either. > > François > > On Jun 10, 2014, at 10:01 AM, hito koto wrote: > > > So, I also have errors: > > > > > > >>> def foo(x): > > ... myList = [] > > ... if isinstance(x, list): > > ... for i in x: > > ... elem = i > > ... return myList + foo(elem) > > ... else: > > ... return 1 > > ... > > >>> > > >>> foo(a) > > Traceback (most recent call last): > > File "", line 1, in > > File "", line 6, in foo > > File "", line 6, in foo > > TypeError: can only concatenate list (not "int") to list > > > > > > > > > > > > 2014年6月10日火曜日 22時53分28秒 UTC+9 François Schiettecatte: > > You are redefining 'list' on line 2, rename list to myList as follows: > > > > def foo(x): > > myList = [] > > if isinstance(x, list): > > for i in x: > > elem = i > > return myList + foo(i) > > else: > > return 1 > > > > And take this to a python list, this is for django. > > > > Cheers > > > > François > > > > > > On Jun 10, 2014, at 9:43 AM, hito koto wrote: > > > > > hi, > > > > > > I have this erroes: > > > > > > >>> def foo(x): > > > ... list = [] > > > ... if isinstance(x, list): > > > ... for i in x: > > > ... elem = i > > > ... return list + foo(i) > > > ... else: > > > ... return 1 > > > ... > > > >>> foo(a) > > > Traceback (most recent call last): > > > File "", line 1, in > > > File "", line 3, in foo > > > TypeError: isinstance() arg 2 must be a class, type, or tuple of classes > > > and types > > > > > > > > > > > > 2014年6月10日火曜日 22時27分42秒 UTC+9 Andrew Farrell: > > > In general, I recommend adding the line "import pdb;pdb.set_trace()" > > > to the top of your function and walking through it to see why it doesn't > > > work. > > > > > > def foo(x): > > > import pdb;pdb.set_trace() > > > > > > list = [] > > > if isinstance(x, list): > > > for i in x: > > > elem = i > > > return list + foo(i) > > > else: > > > return 1 > > > > > > See https://docs.python.org/2/library/pdb.html on how pdb works. > > > There are faster ways to debug, but when starting out, pdb lets you see > > > what is happening as you run the function. > > > > > > > > > Some questions I have about this function: > > > - What is the purpose of the "elem" function? It is never accessed. > > > - What is the purpose of returning 1 if the argument is not a list? > > > - Why is it named "foo" rather than something that tells me what the > > > purpose of the function is? > > > > > > > > > On Tue, Jun 10, 2014 at 8:16 AM, hito koto wrote: > > > Hello, > > > > > > I don't know how can i do to change to write python function > > > I want to following code change to write python function or change to > > > write recursive definition > > > >>> y = [10, 12, [13, [14, 9], 16], 7] > > > >>> z = copy.deepcopy(y) > > > >>> y > > > [10, 12, [13, [14, 9], 16], 7] > > > >>> z > > > [10, 12, [13, [14, 9], 16], 7] > > > >>> z[2][1][1] > > > 9 > > > >>> z[2][1][1] = 88 > > > >>> z > > > [10, 12, [13, [14, 88], 16], 7] > > > [10, 12, [13, [14, 9], 16], 7] > > > >>> > > > > > > this is my use function but not work: > > > > > > def foo(x): > > > list = [] > > > if isinstance(x, list): > > > for i in x: > > > elem = i > > > return list + foo(i) > > > else: > > > return 1 > > > > > > > > > > > > > > > > > > -- > > > You received this message because you are subscribed to the Google Groups > > > "Django users" group. > > > To unsubscribe from this group and stop rec
Re: Re: Django python fuction
Hi, Qiancong : I was looking for exactly this、 Thank you ver much! 2014年6月10日火曜日 23時35分52秒 UTC+9 Qiancong: > > > Hi, hito koto: > I think you want to deep copy the list. The following code maybe helpful: > def dcopy(obj): > if not isinstance(obj, list): > return obj > return [dcopy(x) for x in obj] > > But this function only deep copy for list. You can change code as what I > did for dic, set, tuple , etc .. > -- > moqia...@gmail.com > > *From:* François Schiettecatte > *Date:* 2014-06-10 22:07 > *To:* django-users > *Subject:* Re: Django python fuction > You need to use .append() to add elements to a list, I suggest you take > a look at the python tutorial: > > https://docs.python.org/2/tutorial/ > > Not quite sure what you are doing with i or elem either. > > François > > On Jun 10, 2014, at 10:01 AM, hito koto > > wrote: > > > So, I also have errors: > > > > > > >>> def foo(x): > > ... myList = [] > > ... if isinstance(x, list): > > ... for i in x: > > ... elem = i > > ... return myList + foo(elem) > > ... else: > > ... return 1 > > ... > > >>> > > >>> foo(a) > > Traceback (most recent call last): > > File "", line 1, in > > File "", line 6, in foo > > File "", line 6, in foo > > TypeError: can only concatenate list (not "int") to list > > > > > > > > > > > > 2014年6月10日火曜日 22時53分28秒 UTC+9 François Schiettecatte: > > You are redefining 'list' on line 2, rename list to myList as follows: > > > > def foo(x): > > myList = [] > > if isinstance(x, list): > > for i in x: > > elem = i > > return myList + foo(i) > > else: > > return 1 > > > > And take this to a python list, this is for django. > > > > Cheers > > > > François > > > > > > On Jun 10, 2014, at 9:43 AM, hito koto wrote: > > > > > hi, > > > > > > I have this erroes: > > > > > > >>> def foo(x): > > > ... list = [] > > > ... if isinstance(x, list): > > > ... for i in x: > > > ... elem = i > > > ... return list + foo(i) > > > ... else: > > > ... return 1 > > > ... > > > >>> foo(a) > > > Traceback (most recent call last): > > > File "", line 1, in > > > File "", line 3, in foo > > > TypeError: isinstance() arg 2 must be a class, type, or tuple of > classes and types > > > > > > > > > > > > 2014年6月10日火曜日 22時27分42秒 UTC+9 Andrew Farrell: > > > In general, I recommend adding the line "import pdb;pdb.set_trace()" > > > to the top of your function and walking through it to see why it > doesn't work. > > > > > > def foo(x): > > > import pdb;pdb.set_trace() > > > > > > list = [] > > > if isinstance(x, list): > > > for i in x: > > > elem = i > > > return list + foo(i) > > > else: > > > return 1 > > > > > > See https://docs.python.org/2/library/pdb.html on how pdb works. > > > There are faster ways to debug, but when starting out, pdb lets you > see what is happening as you run the function. > > > > > > > > > Some questions I have about this function: > > > - What is the purpose of the "elem" function? It is never accessed. > > > - What is the purpose of returning 1 if the argument is not a list? > > > - Why is it named "foo" rather than something that tells me what the > purpose of the function is? > > > > > > > > > On Tue, Jun 10, 2014 at 8:16 AM, hito koto > wrote: > > > Hello, > > > > > > I don't know how can i do to change to write python function > > > I want to following code change to write python function or change to > write recursive definition > > > >>> y = [10, 12, [13, [14, 9], 16], 7] > > > >>> z = copy.deepcopy(y) > > > >>> y > > > [10, 12, [13, [14, 9], 16], 7] > > > >>> z > > > [10, 12, [13, [14, 9], 16], 7] > > > >>> z[2][1][1] > > > 9 > > > >>> z[2][1][1] = 88 > > > >>> z > > > [10, 12, [13, [14, 88], 16], 7] > > > [10, 12, [13, [14, 9], 16], 7] > > > >>> > > > > > > this is my use function but not work: > > > > > > def foo(x): > > > list = [] > > > if isinstance(x, list): > > > for i in x: > > > elem = i > > > return list + foo(i) > > > else: > > > return 1 > > > > > > > > > > > > > > > > > > -- > > > You received this message because you are subscribed to the Google > Groups "Django users" group. > > > To unsubscribe from this group and stop receiving emails from it, send > an email to django-users...@googlegroups.com. > > > To post to this group, send email to django...@googlegroups.com. > > > Visit this group at http://groups.google.com/group/django-users. > > > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-users/cf982ef6-1398-4292-9001-eab418f2035a
Re: Re: Django python fuction
Hi, hito koto: I think you want to deep copy the list. The following code maybe helpful: def dcopy(obj): if not isinstance(obj, list): return obj return [dcopy(x) for x in obj] But this function only deep copy for list. You can change code as what I did for dic, set, tuple , etc .. moqianc...@gmail.com From: François Schiettecatte Date: 2014-06-10 22:07 To: django-users Subject: Re: Django python fuction You need to use .append() to add elements to a list, I suggest you take a look at the python tutorial: https://docs.python.org/2/tutorial/ Not quite sure what you are doing with i or elem either. François On Jun 10, 2014, at 10:01 AM, hito koto wrote: > So, I also have errors: > > > >>> def foo(x): > ... myList = [] > ... if isinstance(x, list): > ... for i in x: > ... elem = i > ... return myList + foo(elem) > ... else: > ... return 1 > ... > >>> > >>> foo(a) > Traceback (most recent call last): > File "", line 1, in > File "", line 6, in foo > File "", line 6, in foo > TypeError: can only concatenate list (not "int") to list > > > > > > 2014年6月10日火曜日 22時53分28秒 UTC+9 François Schiettecatte: > You are redefining 'list' on line 2, rename list to myList as follows: > > def foo(x): > myList = [] > if isinstance(x, list): > for i in x: > elem = i > return myList + foo(i) > else: > return 1 > > And take this to a python list, this is for django. > > Cheers > > François > > > On Jun 10, 2014, at 9:43 AM, hito koto wrote: > > > hi, > > > > I have this erroes: > > > > >>> def foo(x): > > ... list = [] > > ... if isinstance(x, list): > > ... for i in x: > > ... elem = i > > ... return list + foo(i) > > ... else: > > ... return 1 > > ... > > >>> foo(a) > > Traceback (most recent call last): > > File "", line 1, in > > File "", line 3, in foo > > TypeError: isinstance() arg 2 must be a class, type, or tuple of classes > > and types > > > > > > > > 2014年6月10日火曜日 22時27分42秒 UTC+9 Andrew Farrell: > > In general, I recommend adding the line "import pdb;pdb.set_trace()" > > to the top of your function and walking through it to see why it doesn't > > work. > > > > def foo(x): > > import pdb;pdb.set_trace() > > > > list = [] > > if isinstance(x, list): > > for i in x: > > elem = i > > return list + foo(i) > > else: > > return 1 > > > > See https://docs.python.org/2/library/pdb.html on how pdb works. > > There are faster ways to debug, but when starting out, pdb lets you see > > what is happening as you run the function. > > > > > > Some questions I have about this function: > > - What is the purpose of the "elem" function? It is never accessed. > > - What is the purpose of returning 1 if the argument is not a list? > > - Why is it named "foo" rather than something that tells me what the > > purpose of the function is? > > > > > > On Tue, Jun 10, 2014 at 8:16 AM, hito koto wrote: > > Hello, > > > > I don't know how can i do to change to write python function > > I want to following code change to write python function or change to write > > recursive definition > > >>> y = [10, 12, [13, [14, 9], 16], 7] > > >>> z = copy.deepcopy(y) > > >>> y > > [10, 12, [13, [14, 9], 16], 7] > > >>> z > > [10, 12, [13, [14, 9], 16], 7] > > >>> z[2][1][1] > > 9 > > >>> z[2][1][1] = 88 > > >>> z > > [10, 12, [13, [14, 88], 16], 7] > > [10, 12, [13, [14, 9], 16], 7] > > >>> > > > > this is my use function but not work: > > > > def foo(x): > > list = [] > > if isinstance(x, list): > > for i in x: > > elem = i > > return list + foo(i) > > else: > > return 1 > > > > > > > > > > > > -- > > You received this message because you are subscribed to the Google Groups > > "Django users" group. > > To unsubscribe from this group and stop receiving emails from it, send an > > email to django-users...@googlegroups.com. > > To post to this group, send email to django...@googlegroups.com. > > Visit this group at http://groups.google.com/group/django-users. > > To view this discussion on the web visit > > https://groups.google.com/d/msgid/django-users/cf982ef6-1398-4292-9001-eab418f2035a%40googlegroups.com. > > > > For more options, visit https://groups.google.com/d/optout. > > > > > > -- > > You received this message because you are subscribed to the Google Groups > > "Django users" group. > > To unsubscribe from this group and stop receiving emails from it, send an > > email to django-users...@googlegroups.com. > > To post to this group, send email to django...@googlegroups.com. > > Visit this group at http://groups.google.com/group/djang