Chapter 2. Programming FAQ 16 Python Frequently Asked Questions, Release 3.9.5 (continued from previous page) Here$_{inc()}$ ,$_{dec()}$ and$_{reset()}$ act like functions which share the same counting variable. In general, try$_{copy.copy()}$ or$_{copy.deepcopy()}$ for the general case. Not all objects can be copied, but most can. Some objects can be copied more easily. Dictionaries have a copy() method: newdict =olddict.copy() Sequences can be copied by slicing: new_l=l[:] For an instance x of a user-defined class,$_{dir(x)}$ returns an alphabetized list of the names containing the instance attributes and methods and attributes defined by its class. Generally speaking, it can’t, because objects don’t really have names. Essentially, assignment always binds a name to a value; the same is true of$_{def}$and$_{class}$statements, but in that case the value is a callable. Consider the following code: >>>classA : ... pass ... >>> B =A >>> a =B() >>> b =a >>> print(b) <__main__.A object at 0x16D07CC> >>> print(a) <__main__.A object at 0x16D07CC> Arguably the class has a name: even though it is bound to two names and invoked through the name B the created instance is still reported as an instance of class A. However, it is impossible to say whether the instance’s name is a or b, since both names are bound to the same value. Generally speaking it should not be necessary for your code to “know the names” of particular values. Unless you are deliberately writing introspective programs, this is usually an indication that a change of approach might be beneficial. In comp.lang.python, Fredrik Lundh once gave an excellent analogy in answer to this question: The same way as you get the name of that cat you found on your porch: the cat (object) itself cannot tell you its name, and it doesn’t really care – so the only way to find out what it’s called is to ask all your neighbours (namespaces) if it’s their cat (object)… ….and don’t be surprised if you’ll find that it’s known by many names, or no name at all! 2.2.14 How can my code discover the name of an object? 2.2.13 How can I find the methods or attributes of an object? 2.2.12 How do I copy an object in Python? def down(self): self.value=self.value-1 count=counter() inc, dec, reset=count.up, count .down, count .set