473,232 Members | 1,538 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,232 software developers and data experts.

python variable assignement


I work at an application witch has embeded python.

We have an python type X.

# a != b

a = b # at this point both variables have the same value

b = select_other()

# steel the same values but both have the new value of b

What might be the cause for this behavior? The type of a and b
variable is the same,
and is defined using PyTypeObject structure.

I hope I was explicit.

Thank you in advance,
Mihai.

Sep 24 '07 #1
4 1274
On Mon, 2007-09-24 at 10:13 +0000, mihai wrote:
I work at an application witch has embeded python.

We have an python type X.

# a != b

a = b # at this point both variables have the same value
Not quite. At this point, 'a' and 'b' are names in the local namespace
that refer to the same object.
b = select_other()
Now 'b' becomes a reference to the object returned by select_other().
Presumably that's a different object than the one it referred to before.
'a' still refers to the same object it referred to before.
# steel the same values but both have the new value of b
No, the code you showed us wouldn't behave this way.
What might be the cause for this behavior?
What behavior?
The type of a and b
variable is the same,
and is defined using PyTypeObject structure.
That doesn't matter. (Variable) Names don't have types. Objects have
types, and a name can refer to an object of any type. See
http://effbot.org/zone/python-objects.htm .
I hope I was explicit.
Unfortunately, you weren't. If the above explanations didn't help you,
please explain what you want to achieve, show us your actual code and
what you expect it to do, and tell us what it's doing instead.

--
Carsten Haese
http://informixdb.sourceforge.net
Sep 24 '07 #2

This is the code:

begin = select_point()
if begin == None:
return

end = select_point()
while end != None:
record = Record(begin, end)
id = add(record)
begin = end
end = select_point()
# here (sometimes) begin has the same value (or points to the
same object) like end, the newly selected one

Is there a way to see if the names points to the same variables or
that there are different variables with the same values?

The problem is that the problem is more like an bug,
it happens only in certain conditions, and I have no idea why.

I have checked the values returned by select_point() and are different
in all the cases,
so the problem is with that variables names/values.

Thank you again,
Mihai.

Sep 24 '07 #3
On Mon, 2007-09-24 at 12:12 +0000, mihai wrote:
[...]
id = add(record)
[...]
Not that this causes your problem, but I'd still like to point out that
'id' is the name of a built-in function. Shadowing built-in names can
lead to surprising behavior.
Is there a way to see if the names points to the same variables or
that there are different variables with the same values?
Yes. "==" tests whether two objects are equal, whereas "is" tests
whether two objects are actually the same object. Examples:

Two different list objects with equal contents:
>>a = [1,2,3]
b = [1,2,3]
a==b
True
>>a is b
False

Two names for the same list object:
>>a = [1,2,3]
b = a
a==b
True
>>a is b
True

Hope this helps,

--
Carsten Haese
http://informixdb.sourceforge.net
Sep 24 '07 #4
mihai <Mi*********@gmail.comwrote:
>
This is the code:

begin = select_point()
if begin == None:
return

end = select_point()
while end != None:
record = Record(begin, end)
id = add(record)
begin = end
end = select_point()
# here (sometimes) begin has the same value (or points to the
same object) like end, the newly selected one

Is there a way to see if the names points to the same variables or
that there are different variables with the same values?
You can check whether two names refer to the same object with the 'is'
operator. So you would use:

if begin is end: continue

to skip over any duplicate
>
The problem is that the problem is more like an bug,
it happens only in certain conditions, and I have no idea why.

I have checked the values returned by select_point() and are different
in all the cases,
so the problem is with that variables names/values.
Are you sure that nothing you do can change the list of points you are
iterating over: usually iteration returning unexpected duplicates is
because you inserted something new into the middle of the list.

A few other unrelated points: the convention is to use 'is' when
checking for None, and you can reduce the number of calls to
'select_point' if you use the 'iter' function. Putting those together:

begin = select_point()
if begin is None:
return

for end in iter(select_point, None):
if begin is end:
continue
record = Record(begin, end)
id = add(record)
begin = end
Sep 24 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: yuraukar | last post by:
I am trying to create a garbage collection class in C++ to collect instances of a specific class (so no general gc). The approach is to use smart pointers and a mark and a simple sweep gc. ...
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
15
by: pranab_bajpai | last post by:
So I want to define a method that takes a "boolean" in a module, eg. def getDBName(l2): .... Now, in Python variables are bound to types when used, right? Eg. x = 10 # makes it an INT...
3
by: sushant | last post by:
hi all, whats the difference between assignement and initialisation in C. is assignement related with scanf() and initialisation with (int x=10;) sushant
4
by: Michael Yanowitz | last post by:
I am still new to Python but have used it for the last 2+ months. One thing I'm still not used to is that functions parameters can't change as expected. For example in C, I can have status =...
34
by: glomde | last post by:
i I would like to extend python so that you could create hiercical tree structures (XML, HTML etc) easier and that resulting code would be more readable than how you write today with packages like...
17
by: Alexander Eisenhuth | last post by:
Hello, is there a assignement operator, that i can overwrite? class MyInt: def __init__(self, val): assert(isinstance(val, int)) self._val = val a = MyInt(10)
145
by: Dave Parker | last post by:
I've read that one of the design goals of Python was to create an easy- to-use English-like language. That's also one of the design goals of Flaming Thunder at http://www.flamingthunder.com/ ,...
7
by: John Doe | last post by:
Hi, I am trying to replace the use of the Windows CString class by a compatible one (CStdString) using std::string , the problem is I cannot do the following thing : A) CString strFullPath;...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.