3

I would to know what Python call when I use the =:

a = b

Where do I look for this information?

I would have the "assignment to variables" with my =

a would have a similar behaviour

l=list()  
l.append(1)  
l.append(2)  
l.append(3)  
l1=l  
l1[2] = ’B’  
print(l1)  
[1, 2, ’B’]  
print(l)  
[1, 2, 3]
fege
  • 547
  • 3
  • 7
  • 19
  • 5
    Why do you want to redefine the assignment operation? That's just asking for trouble. – Jonathan M Feb 16 '12 at 20:08
  • 3
    Is this one of those times when you can obnoxiously yell **"THAT'S NOT *PYTHONIC!!!*"** without a care what "pythonic" means? – BoltClock Feb 16 '12 at 20:08
  • Serious question: What would you use it for? – AndiDog Feb 16 '12 at 20:16
  • 1
    @BoltClock - maybe you don't know what "pythonic" means yet, but you at least are recognizing practices that are *not* pythonic. – PaulMcG Feb 16 '12 at 20:17
  • I would have the "assignment to variables" with my = – fege Feb 16 '12 at 20:33
  • "I would have the "assignment to variables" with my ="? What does this mean? Python already assigns variables with `=`. Since Python already does this, what are you talking about? Are you confused about the semantics of assignment? – S.Lott Feb 16 '12 at 20:40
  • la=['a','b','c'] la1=la la1[1]='h' i want have two lists one la->['a','b','c'] and la1->['a','h','c'] – fege Feb 16 '12 at 20:46
  • @fege: Simply copy the list: `la1 = la[:]`. Also look at the `copy` module if you want to copy almost arbitrary objects. – Sven Marnach Feb 16 '12 at 20:51
  • i know that but i want redefine = for make automatic this, if it is possible – fege Feb 16 '12 at 20:53
  • "i want redefine = for make automatic this"? You want `=` to magically do list copy? Do you realize how many things will break and be unusable because of this? You would probably break **most** of the built-in library. Parts of Python require sharing list objects. – S.Lott Feb 16 '12 at 21:02

5 Answers5

13

You can't redefine = in Python. It will always bind the object on the right-hand side to the name on the left-hand side.

Note that this is quite different from e.g. C++, where the = operator typically involves copying data to the target variable. Python does not have variables in the sense C++ has. Python has names that can be bound to objects.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
9

You can't redefine =, but you can redefine:

a[c] = b
   or
a.c  = b

Do this by implementing __setitem__ or __setattr__, respectively. For attributes, it's often more appropriate to use property, but __setattr__ has its uses.

kenm
  • 23,127
  • 2
  • 43
  • 62
5

You cannot override = in Python. You can see the list of special methods that you can override in the documentation and there's nothing to match = on that list.

Python always binds a name in your namespace to a value. This means that Python does't have "assignment to variables", it only has "binding to values": there's no data being copies, instead another reference is being added to the same value.

Martin Geisler
  • 72,968
  • 25
  • 171
  • 229
0

You can override it, if you are inside a class.

For example:

class A(object):
    def __setattr__(self,name,value):
        print 'setting', name, 'to', value

Then:

A().foo = 'bar'

Would output:

setting foo to bar

Keep in mind, this would only modify that one class, not your entire program.

campos.ddc
  • 741
  • 5
  • 12
  • if this object is on the **LHS** of the assignment operation, yes. But not on the **RHS** – smci Mar 15 '13 at 04:48
0

Or maybe you can do in this way:

def funct(obj):  
        import copy  
        print('entro')  
        return str(copy.deepcopy(obj))   
class metacl(type):  
        def __new__(meta,classname,supers,classdict):  
                classdict['__xxx__'] = funct  
                return type.__new__(meta,classname,supers,classdict)  
class list(list,metaclass=metacl): pass

I do not know which built-in function you must ovverride (xxx). It is the unique way to use a metaclass, i think.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
postgres
  • 2,242
  • 5
  • 34
  • 50