Re: How to test if an object IS another object?
[EMAIL PROTECTED] wrote: > > Tuples (which are immutable) also appear to be reused > > foo = () bar = () foo is bar > > True Not always: foo = (1,) bar = (1,) foo is bar => False -- bruno desthuilliers python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for p in '[EMAIL PROTECTED]'.split('@')])" -- http://mail.python.org/mailman/listinfo/python-list
Re: How to test if an object IS another object?
This isn't a good example to test with, since 3 is an immutable object, as is 300 and all ints. It's more meaningful if the objects are mutable. Why do you want to test identity in the first place? Roose <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Sorry about removing my message, I posted with the wrong google > account, I don't really want my email where those irritating spam bots > can find it. > >>The most obvious way (as usual ?): >> >>if obj1 is obj2: >> // your code here > > I immediately thought of is, and tested it in the console, but it > didn't work quite like I expected: > >>foo = 3 >>bar = 3 >>zoo = foo >>foo is zoo > True >>foo is bar > True >>zoo is bar > True > > clearly foo and bar have the same value but they are different objects > aren't they? Yet applying the is operator yields True. > > Thanks, > -Dan > -- http://mail.python.org/mailman/listinfo/python-list
Re: How to test if an object IS another object?
On 2005-06-12, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Fascinating. With small strings, it uses the same object, and with > small numbers like 3. With 300 they were different objects (why, It's purely an implimentation detail. The small integers get used a lot, so Python keeps a pre-created set of small integers handy. It would be a bit, uh, wasteful to pre-create all of possible integer objects, so "large" integers get created on the fly without checking to see if there are any existing ones with the right value. Large integers could get cached and re-used, but that would be extra overhead with little chance for benefit. > shouldn't they both be ints still?) They are. -- Grant Edwards grante Yow! .. over in west at Philadelphia a puppy is visi.comvomiting... -- http://mail.python.org/mailman/listinfo/python-list
Re: How to test if an object IS another object?
On 2005-06-12, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: >>The most obvious way (as usual ?): >> >>if obj1 is obj2: >> // your code here > > I immediately thought of is, and tested it in the console, but it > didn't work quite like I expected: > >>foo = 3 >>bar = 3 >>zoo = foo >>foo is zoo > True >>foo is bar > True >>zoo is bar > True > > clearly foo and bar have the same value but they are different objects > aren't they? Nope. > Yet applying the is operator yields True. They're the same object. Why did you expect them not to be? -- Grant Edwards grante Yow! Am I elected yet? at visi.com -- http://mail.python.org/mailman/listinfo/python-list
Re: How to test if an object IS another object?
Fascinating. With small strings, it uses the same object, and with small numbers like 3. With 300 they were different objects (why, shouldn't they both be ints still?) Mutable objects functioned differently as you suggested: >>>foo = [] >>>bar = [] >>>foo == bar True >>>foo is bar False Tuples (which are immutable) also appear to be reused >>>foo = () >>>bar = () >>>foo is bar True Thanks for your help, I know how to solve the problem now. -Dan -- http://mail.python.org/mailman/listinfo/python-list
Re: How to test if an object IS another object?
[EMAIL PROTECTED] a écrit : > Sorry about removing my message, I posted with the wrong google > account, I don't really want my email where those irritating spam bots > can find it. > > >>The most obvious way (as usual ?): >> >>if obj1 is obj2: >> // your code here > > > I immediately thought of is, and tested it in the console, but it > didn't work quite like I expected: > > >>foo = 3 >>bar = 3 >>zoo = foo >>foo is zoo > > True > >>foo is bar > > True > >>zoo is bar > > True > > clearly foo and bar have the same value but they are different objects > aren't they? Nope. They are two different names bound to the same integer object. You may have similar situation with strings: >>> s1 = "toto" >>> s2 = "toto" >>> s1 is s2 True This is an application of the lightweight pattern. The Python interpreter reuse the same "value object" to avoid memory clutter. Since ints and strings are immutable, this is perfectly safe (but yet confusing when you're not aware of this). > Yet applying the is operator yields True. Yes. But now you know why !-) And don't worry, this is quite unlikely that it will cause you any trouble in real code. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to test if an object IS another object?
You can use the id() function to test equality of objects: [EMAIL PROTECTED]:~$ python Python 2.4.1 (#2, Mar 30 2005, 21:51:10) [GCC 3.3.5 (Debian 1:3.3.5-8ubuntu2)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> a = 3 >>> b = 3 >>> id(a) 135585176 >>> id(b) 135585176 >>> -- http://mail.python.org/mailman/listinfo/python-list
Re: How to test if an object IS another object?
[EMAIL PROTECTED] writes: > >foo = 3 > >bar = 3 > clearly foo and bar have the same value but they are different objects > aren't they? No, they're the same object. Now try it with 300 instead of 3 ;-). -- http://mail.python.org/mailman/listinfo/python-list
Re: How to test if an object IS another object?
Sorry about removing my message, I posted with the wrong google account, I don't really want my email where those irritating spam bots can find it. >The most obvious way (as usual ?): > >if obj1 is obj2: > // your code here I immediately thought of is, and tested it in the console, but it didn't work quite like I expected: >foo = 3 >bar = 3 >zoo = foo >foo is zoo True >foo is bar True >zoo is bar True clearly foo and bar have the same value but they are different objects aren't they? Yet applying the is operator yields True. Thanks, -Dan -- http://mail.python.org/mailman/listinfo/python-list
Re: How to test if an object IS another object?
[EMAIL PROTECTED] a écrit : > If two objects are of equal value you can compare them with ==. What I > want to do is find out if two objects are actually just references to > the same object, how can I do this in Python? The most obvious way (as usual ?): if obj1 is obj2: // your code here -- http://mail.python.org/mailman/listinfo/python-list
How to test if an object IS another object?
If two objects are of equal value you can compare them with ==. What I want to do is find out if two objects are actually just references to the same object, how can I do this in Python? Thanks -- http://mail.python.org/mailman/listinfo/python-list