[Tutor] cases with single if and an else clause

Manprit Singh manpritsinghece at gmail.com
Fri Oct 8 05:28:34 EDT 2021


Once again a big thanks to Dennis for a very very informative email , That
made me to do some experiments with the python interpreter as given below:

>>> a = 1
>>> b = a
>>> b
1
>>> a
1
>>> id(a)
140287237945648
>>> id(b)
140287237945648
>>>
Here in this example above due to the assignment in the second line ,
variables a and b are now references to the same object .  Which is proved
by their id, id(a) is the same as id(b).

>>> a = [3, 6, 7]
>>> b= a
>>> id(a), id(b)
(140287234775168, 140287234775168)
>>> a[0] = 7
>>> a
[7, 6, 7]
>>> b
[7, 6, 7]
>>>
In this example again a & b variables are references to the same object ,
Since lists are mutable doing a[0] = 7  makes changes in a & b
Second thing is arguments are passed by assignment in Python  in functions.

  def increment(x):
      return x+1

num = 9
ans = increment(num)
When we execute it , the value of num will be passed to formal parameter x
by assignment, so obviously no copy is made, x is reference to num , the
function will return a value incremented by one . One point written in
Dennis' email I do feel is correct is if we place a bulky data structure
inside the function, everytime the function is called, there will be an
overhead of creation and destruction of this data structure.

So placing the data structure outside the function is obviously good :
1) First to avoid overheads as given above
2) If we need to use that data structure in the program, we would not be
able to use it if it was placed locally inside the function as mentioned by
Alan's sir email.

Hopefully my points are correct now
Need comments

Regards

Manprit Singh


On Thu, Oct 7, 2021 at 5:05 AM Dennis Lee Bieber <wlfraed at ix.netcom.com>
wrote:

> On Thu, 7 Oct 2021 03:53:11 +0530, Manprit Singh
> <manpritsinghece at gmail.com> declaimed the following:
>
>
> >def grades(glist, score):
>
> >
> >lst = [("F", 60),
> >       ("D", 70),
> >       ("C", 80),
> >       ("B", 90),
> >       ("A", 101)]
> >
> >try:
> >    marks= int(input("Enter marks"))
> >    grade = grades(lst, marks)
> >
>
> >
> >My Next question is, in this particular scenario, where keeping the
> >variable lst (which contains grade Characters and marks limits) as global
> >and passing it as an argument to the function grades. So when this
> function
> >is called, a copy of lst is created and it is destroyed upon exit from
> >function.
>
>         There is NO COPY... The parameter NAME "glist" is bound to the
> actual
> list.
>
> >>> lst = []
> >>> def aFunc(lst):
> ...     lst.append(len(lst))
> ...
> >>> lst
> []
> >>> aFunc(lst)
> >>> lst
> [0]
> >>>
>
>         Names in Python are bound (connected) to objects. Python does NOT
> use
> the "post office box" model where the names are fixed memory locations and
> assignment copies the contents of the source box to the destination box.
> Instead, in a somewhat simplified representation, Python uses "Post-It"
> notes attached to strings which themselves connect to objects laying on the
> floor. "Assignment" (binding) grabs a Post-It, writes the destination name
> on it, finds a free string, attaches the Post-It (name) to the string, then
> finds the source name's Post-It, follows its string to the relevant object,
> and attaches the destination's string to the same object.
>
> >>> aFunc(lst[:])
> >>> lst
> [0]
> >>>
>
>         The use of [:] is what MAKES A (shallow) COPY of a list.
>
> >
> >As the variable lst serves no other purpose, why not keep this lst as a
> >local variable inside the function grades .
>
>         Python is byte-code interpreted at run time. Putting the list
> creation
> inside the function means that the list is created each time the function
> is executed, and deleted when the function exits. If the list (or other
> structure) is large, creating it each time could be a significant slow-down
> on the program.
>
> >>> def bFunc():
> ...     lst = [a]
> ...     lst.append(len(lst))
> ...     return lst
> ...
> >>> a = 1
> >>> bFunc()
> [1, 1]
> >>> a = 3
> >>> bFunc()
> [3, 1]
> >>>
>
>         If the list were not built at run time, the "def bFunc" compile (to
> byte code) would fail since at the time, "a" does not exist.
>
>         I used all capitals as a convention in Python that this name
> represents
> a CONSTANT and should not be rebound or modified by code; code should only
> access (read) the contents. I also treated it as a global (within the file)
> constant to avoid passing it.
>
>         I would pass it if I were generating the score<>letter break points
> dynamically (my example of "grading by the curve") as then it is not a
> constant..
>
>
>
> --
>         Wulfraed                 Dennis Lee Bieber         AF6VN
>         wlfraed at ix.netcom.com
> http://wlfraed.microdiversity.freeddns.org/
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list