Someone said to me Everything in python is an object. Like the example below, python just points to the same address for the same value. a = 1 b = 1 print(id(a), id(b)) # Same! print(a is b) # True  But this rule seems like not being applied a list. a = [1] b = [1] print(id(a), id(b)) # != print(a is b) # False  I tested this for a Tuple, but it also didn't work. So I guess it's not about whether they are mutables or not. Why does this happen?
2846
Why does the following behave unexpectedly in Python?  &gt;&gt;&gt; a = 256 &gt;&gt;&gt; b = 256 &gt;&gt;&gt; a is b True           # This is an expected result &gt;&gt;&gt; a = 257 &gt;&gt;&gt; b = 257 &gt;&gt;&gt; a is b False          # What happened here? Why is this False? &gt;&gt;&gt; 257 is 257 True           # Yet the literal numbers compare properly   I am using Python 2.5.2. Trying some different versions of Python, it appears that Python 2.3.3 shows the above behaviour between 99 and 100.  Based on the above, I can hypothesize that Python is internally implemented such that "small" integers are stored in a different way than larger integers and the is operator can tell the difference. Why the leaky abstraction? What is a better way of comparing two arbitrary objects to see whether they are the same when I don't know in advance whether they are numbers or not?
17001
I'm wondering where I find the source to show how the operator ** is implemented in Python.  Can someone point me in the right direction?
89893
The __debug__ variable is handy in part because it affects every module. If I want to create another variable that works the same way, how would I do it?  The variable (let's be original and call it 'foo') doesn't have to be truly global, in the sense that if I change foo in one module, it is updated in others. I'd be fine if I could set foo before importing other modules and then they would see the same value for it.
53911
I've been trying to wrap my head around the basic concepts of category theory, and I thought I would attempt to illustrate what I understand with the category of sets, probably the easiest example. Particularly, I've been trying to prove that $id_A$ (the identity morphism on $A$, for all $A \in Obj(\mathbf{Set})$) is $1_A \colon A \rightarrow A, x \mapsto x$.  This is a very intuitive and reasonable statement, and it's trivial to prove that $1_A$ is indeed an identity morphism on $A$, and I suppose uniqueness of $id_A$ can be demonstrated analogously to uniqueness of the identity element in a monoid (considering the subcategory which has $A$ as its only object, and endofunctions on $A$ as its only morphisms).  In this manner, it is not hard to prove that the proposition in the title is true, but this demonstration requires to make an assumption or guess as to what could $id_A$ be. Specifically, the scheme of the proof is: assume $id_A$ = $1_A$, see that it works with the definition of an identity morphism, show that the identity morphism is unique, and, in conclusion, $id_A$ can only be $1_A$. What I'm looking for, nonetheless, is a somehow more direct proof, that doesn't assume $id_A$ = $1_A$ at the start. I want to place myself in a state of little or no knowledge about sets and functions, and under this assumption, why would I assume $id_A$ = $1_A$ at first? Why not try with $id_\mathbb{Z}$ = $f \colon \mathbb{Z} \to \mathbb{Z}, x \mapsto x^2 + 1$, for example? It wouldn't work, but I don't have any reason to think that $1_\mathbb{Z}$ is a better guess for $id_\mathbb{Z}$.  I suppose that the proof for which I'm asking would work for categories of sets with additional structure, and probably for posets as well, although I'm not clear as to what modifications it would require to work.  Thanks.
56376
I'm trying to get my head around mutable vs immutable objects. Using mutable objects gets a lot of bad press (e.g. returning an array of strings from a method) but I'm having trouble understanding what the negative impacts are of this. What are the best practices around using mutable objects? Should you avoid them whenever possible?
69276
Learning Objective-C and reading sample code, I notice that objects are usually created using this method:  SomeObject *myObject = [[SomeObject alloc] init];   instead of:  SomeObject *myObject = [SomeObject new];   Is there a reason for this, as I have read that they are equivalent?
64500
Currently, I'm trying to get a method in Python to return a list of zero, one, or two strings to plug into a string formatter, and then pass them to the string method. My code looks something like this:  class PairEvaluator(HandEvaluator):   def returnArbitrary(self):     return ('ace', 'king')  pe = PairEvaluator() cards = pe.returnArbitrary() print('Two pair, {0}s and {1}s'.format(cards))   When I try to run this code, the compiler gives an IndexError: tuple index out of range. How should I structure my return value to pass it as an argument to .format()?
95092
Why does list.index throw an exception, instead of using an arbitrary value (for example, -1)? What's the idea behind this?  To me it looks cleaner to deal with special values, rather than exceptions.  EDIT: I didn't realize -1 is a potentially valid value. Nevertheless, why not something else? How about a value of None?
98914
The MSDN documentation on  describes 3 contradicting rules for how the method should work.     If two objects of the same type represent the same value, the hash function must return the same constant value for either object. For the best performance, a hash function must generate a random distribution for all input. The hash function must return exactly the same value regardless of any changes that are made to the object.   Rules 1 &amp; 3 are contradictory to me.  Does Object.GetHashCode() return a unique number based on the value of an object, or the reference to the object. If I override the method I can choose what to use, but I'd like to know what is used internally if anyone knows.
2603
I have been bitten by something unexpected recently. I wanted to make something like that:  try :      thing.merge(iterable) # this is an iterable so I add it to the list except TypeError :      thing.append(iterable) # this is not iterable, so I add it   Well, It was working fine until I passed an object inheriting from Exception which was supposed to be added.  Unfortunetly, an Exception is iterable. The following code does not raise any TypeError:  for x in Exception() :     print 1   Does anybody know why?
56817
I have alot of questions.   Do Infinite sets have the same cardinality when there is a bijection between them? Are $\mathbb{N}$ and $\mathbb{Z}$ infinite sets? I assume they are, but why? Why does there not exist a bijection between them? Why do $\mathbb{N}, \mathbb{Z}, \mathbb{Q}$ have the same cardinality? I assume there exists a bijection between them, but how can I find this function?
108429
When are objects or something else said to be "first class" in a given programming language, and why? In what do they differ from languages where they are not?  EDIT. When one says "everything is an object" (like in Python), does he indeed mean that "everything is first-class"?
75345
I have an object following a NurbsPath (path is the parent of the tracker object)  and I am trying to print its position in each frame. but the "location" attribute always print the same value, doesn't matter in which position over the path the object is.  I guess I am querying the wrong property.. this is the code I am using:   import bpy  sce = bpy.data.scenes[0]  obj = bpy.data.objects["tracker0"] print(obj.location[0])  for i in range(0, 20):     sce.frame_current = i     print(obj.location)
3181
When writing custom classes it is often important to allow equivalence by means of the == and != operators. In Python, this is made possible by implementing the __eq__ and __ne__ special methods, respectively. The easiest way I've found to do this is the following method:  class Foo:     def __init__(self, item):         self.item = item      def __eq__(self, other):         if isinstance(other, self.__class__):             return self.__dict__ == other.__dict__         else:             return False      def __ne__(self, other):         return not self.__eq__(other)   Do you know of more elegant means of doing this? Do you know of any particular disadvantages to using the above method of comparing __dict__s?  Note: A bit of clarification--when __eq__ and __ne__ are undefined, you'll find this behavior:  &gt;&gt;&gt; a = Foo(1) &gt;&gt;&gt; b = Foo(1) &gt;&gt;&gt; a is b False &gt;&gt;&gt; a == b False   That is, a == b evaluates to False because it really runs a is b, a test of identity (i.e., "Is a the same object as b?").  When __eq__ and __ne__ are defined, you'll find this behavior (which is the one we're after):  &gt;&gt;&gt; a = Foo(1) &gt;&gt;&gt; b = Foo(1) &gt;&gt;&gt; a is b False &gt;&gt;&gt; a == b True
16116
When using IF statements in Python, you have to do the following to make the "cascade" work correctly.  if job == "mechanic" or job == "tech":         print "awesome" elif job == "tool" or job == "rock":         print "dolt"   Is there a way to make Python accept multiple values when checking for "equals to"? For example,  if job == "mechanic" or "tech":     print "awesome" elif job == "tool" or "rock":     print "dolt"
110095
It seems to me that tuples are just as elementary as sets. Can tuples be reduced to sets?
110574
So, I want to define a singleton method for an object, but I want to do it using a closure.  For example,  def define_say(obj, msg)   def obj.say     puts msg   end end  o = Object.new define_say o, "hello world!" o.say   This doesn't work because defining a singleton method via "def" is not a closure, so I get an exception that "msg" is an undefined variable or method.  What I would like to do is something like using the "define_method" method in the Module class, but as far as I can tell, this can only be used to define a method on a class... but I want a Singleton Method...  So, I would love to write it something like this:  def define_say(obj, msg)   obj.define_singleton_method(:say) {     puts msg   } end   Does anyone know how I can achieve this without having to create a method to store a Proc and then use the Proc within a singleton method? (basically, I want a clean, non-hacky way of doing this)