Re: [Tutor] Calling class from another class

2018-05-24 Thread Peter Otten
aishwarya selvaraj wrote:

>  Dear all,
>  I have created 2 classes in 2 separate files.File 1 named atcore_py.pyx
> with class andorCameraSDK3, and file 2 with name  AndorCameraGUI making
> use of TKinter.
> I was able to import  andorCameraSDK3 into  AndorCameraGUI, but I was not
> able to do the other way around, as I need to call the function Plot() in
> file 2 inside function acquireimage() in file 1.
> When I define self.master = master in file 1 and pass this from file 2 as
> self.camera =  andorCameraSDK3(self) , I get an error saying :
> 
>> ​ Exception in Tkinter callback
>>
> 
> Traceback (most recent call last):
>>
> File "/home/ravindra/anaconda2/lib/python2.7/lib-tk/Tkinter.py", line
> 1541,
>> in __call__
>>
> 
> return self.func(*args)
>>
> File "/home/ravindra/PycharmProjects/LiveMode/GUI.py", line 278, in
>> getCameraStringGU
>>
> 
> self.camera = AndorCameraSDK(self)
>>
> File "atcore_py.pyx", line 77, in AndorCameraDriver.
>> andorCameraSDK3.__cinit__
>>
> self.master = master
>>
> AttributeError: 'AndorCameraDriver.andorCameraSDK3' object has no
> attribute
>> 'master'

The attachments didn't make it, but it looks like you are using Cython.

According to

http://cython.readthedocs.io/en/latest/src/userguide/extension_types.html#dynamic-attributes

you cannot add arbitrary attributes to a class written in Cython, you have 
to declare them:

cdef class andorCameraSDK3:
cdef object master
...

> Process finished with exit code 0  ​
>>
>>
> 
> ​
> I'm attaching both the files along with this email. It would be great if
> someone could help me out here. Thanks in advance
> 
> ​
> 


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Nested use of replication operator on lists

2018-05-24 Thread boB Stepp
On Python-list Steve started a thread, "List replication operator"
(https://mail.python.org/pipermail/python-list/2018-May/733513.html)
and wrote the following:


Python has a sequence replication operator:

py> [1, 2]*3
[1, 2, 1, 2, 1, 2]


Unfortunately, it is prone to a common "gotcha":

py> x = [[]]*5  # make a multi-dimensional list
py> x
[[], [], [], [], []]
py> x[0].append(1)
py> x
[[1], [1], [1], [1], [1]]

The reason for this behaviour is that * does not copy the original list's
items, it simply replicates the references to the items. So we end up
with a new list containing five references to the same inner list.


I am having trouble correlating the behavior of the one-dimensional
case with the two-dimensional case.  The result of [1, 2]*3 seems to
be an actual list, not a replication of the references to the items in
the original list, [1, 2].  Or if it is, then I do not know how to
demonstrate it.

Also the "replication operator" does not seem to be replicating
anything list-wise if it is instead replicating references to the
original list's members.

I request explanation/clarification please.


-- 
boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Nested use of replication operator on lists

2018-05-24 Thread Danny Yoo
Each value in Python has an associated numeric address associated to it.
We can probe for it:

  https://docs.python.org/3/library/functions.html#id

For example:

#
>>> x = [1, 2, 3]
>>> y = x[:]
>>> id(x)
139718082542336
>>> id(y)
139718082556776
#

Here, we create a list and point the name 'x' to it.  We point another
name, 'y', to a slice of the first list.  Slicing creates a new list.

Let's make one more name:

#
>>> z = x
>>> id(z)
139718082542336
#

Note that the numeric id() that we get from 'z' is the same number as the
id() we get from 'x'.  These indicate that both names are referring to the
identical list value.


Identity matters because some values can be changed, or mutated.  It means
that two values can start looking the same, like the two lists that we
created:

#
>>> x
[1, 2, 3]
>>> y
[1, 2, 3]
#

but after a mutation:

##
>>> x.append('four')
>>> x
[1, 2, 3, 'four']
>>> y
[1, 2, 3]
##

we can see that they are now different.


If we try using 'z', we see:

##
>>> z
[1, 2, 3, 'four']
##


and that squares with what we said earlier: we gave the list we see here
two names: 'x' and 'z'.  But it's the same list.


To come to your example, to get an intuition of what's happening, try
applying id() on individual elements of the list, to probe which ones are
unique and which ones are the same.  You can also use a graphical tool like
pythontutor.com.  Take a look:  https://goo.gl/HBLTw9


A slight revision to the scenario might make things a little clearer.  Try:

##
nested_lists = [['a'], ['b']] * 2
first_element = nested_lists[0]
second_element = nested_lists[1]

first_element.append('c')
second_element.append('d')
##

Visualization: https://goo.gl/k4TLvi


See if the graphical view squares away with your internal mental model.
Please feel free to ask questions.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Nested use of replication operator on lists

2018-05-24 Thread Steven D'Aprano
On Thu, May 24, 2018 at 10:39:17PM -0700, Danny Yoo wrote:

> Each value in Python has an associated numeric address associated to it.

No they don't :-)

Each object in Python has an arbitrary numeric ID associated with it. 
The Python language has no supported way to get the address of an 
object. The id() function returns an opaque and officially meaningless 
ID number, that is all.

It so happens that CPython uses the (memory) address of the object as 
that ID number, but not all Python interpreters do the same. Jython and 
IronPython, for example, assign IDs in consecutive order, so you will 
get IDs like 20, 21, 22, 23, 24, etc.

(By memory, IronPython allocates the ID when the object is first 
created, while Jython allocates an ID only when you ask for one.)

The reason IronPython and Jython do that is because they run inside an 
environment where objects can be moved around memory by the memory 
manager and garbage collector. So if an object X has address 1234 at one 
moment, a little later it could be moved to address 9516. Using the 
address would not be stable.



-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor