names
and bindings
."On Thu, Feb 21, 2013 at 10:24 PM, Aditya Athalye <aditya....@gmail.com> wrote:
I'm really liking this recent blog post by @JeffKnupp. Thought it will be
interesting for others too... Definitely one for the (my) bookmarks:
"Drastically Improve Your Python: Understanding Python's Execution Model"
http://www.jeffknupp.com/blog/2013/02/14/drastically-improve-your-python-und
erstanding-pythons-execution-model/
The only way an object becomes immutable is by the author not introducing in mutating methods.
On Fri, Feb 22, 2013 at 2:23 PM, Noufal Ibrahim <nou...@gmail.com> wrote:
Dhananjay Nene <dhananj...@gmail.com> writes:
[...]
I presume you're talking about user defined objects? Strings in Python
> Another place where it got a bit confusing was in suggesting different
> types of objects mutable and immutable. AFAIK python has zero support for
> immutable objects at a language level (one of my top 5 peeves with the
> lang).
are immutable. Are they not?
I was talking of "at a language level". The implementation of strings makes it immutable because it has no mutating methods and also because it is kind of hard to monkeypatch it as well. There are unlikely any declarations in the string class (haven't seen it myself) which tell the language runtime please make my instances immutable. For a language truly supporting immutability I imagine there should be some support for declaring an instance as immutable (alternatively having immutable instances by default and thus declaring some instances as mutable or not at all).User defined objects also do not become immutable by just offering non mutating methods. They also need to capture setattr / getattr to ensure someone doesn't monkeypatch them away.But thats like having a personal security system to thwart any intruders as opposed to an environment of low crime :)
I'm not sure how you can choose to define immutability in your terms and then claim that the language doesn't support immutability. Yeah, the language doesn't offer 'implicit' facilities to declare an object as immutable, but that doesn't mean, immutability doesn't exist.
mutable
and immutable
."Quote: "Instead of variables (in the classic sense), Python hasnames
andbindings
."What?? Instead of talking about call by value or call by reference semantics (or suggesting java & python essentially pass a copy of a reference to but not a deep copy of the value), the post goes into a completely strange conversation called names vs. bindings.
names
and bindings
." I of course also need to figure out how names and bindings encompass call by value because that is something thats escaping me at the moment.>That was a typo - he was talking about variables vs. bindings. But did not issue a correction since I thought anyone would obviously figure out
> that the post referred to a dichotomy between variables and (names and bindings).What was a typo? I don't see anything in the article that conveys anything wrong about names or bindings.
--
Yati Sagade
Software Engineer at mquotient
Twitter: @yati_itay | Github: yati-sagade
Organizing member of TEDx EasternMetropolitanBypass
http://www.ted.com/tedx/events/4933
https://www.facebook.com/pages/TEDx-EasternMetropolitanBypass/337763226244869
--
You received this message because you are subscribed to the Google Groups "Python Pune" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pythonpune+...@googlegroups.com.
To post to this group, send email to pytho...@googlegroups.com.
Visit this group at http://groups.google.com/group/pythonpune?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
On Friday 22 February 2013 03:24 PM, Dhananjay Nene wrote:
<mailto:st...@lonetwin.net>> wrote:
I'm not sure how you can choose to define immutability in your terms and
then claim that the language doesn't support immutability. Yeah, the
language doesn't offer 'implicit' facilities to declare an object as
immutable, but that doesn't mean, immutability doesn't exist.
The language does not offer "explicit" facilities to declare an object as
immutable (eg. const in C++)
To quote the post: "It turns out Python has two flavors of objects:
|mutable| and |immutable|."
What it seems to imply is not true. I cannot create a class and then tell the
language runtime it is immutable. I cannot create an instance and pass it to
another function and say I expect it to be not modified.
When I hear a language supports immutability I expect some explicit support in
the language to support immutability. I would love to be able to define a class
like the tuple, and then tell the language it should not be modified. A
hypothetical version of python that in my mind supported immutability would
automatically reject any setattr calls on i
Look at the code in my previous post or the one below
I think you should explore if you do find any difference in "existence of
mutable and immutable objects" and "a language having two flavours of objects
mutable and immutable". To me "a language having two flavours of objects
suggests someone being able to tell the language I want instances of a class or
a particular instance or a particular reference being passed to a function to be
immutable. Otherwise in my mind it the language does not support immutability
(although it merely allows its existence). Is that redefinition of immutability
on my terms ? If you still think so, I have made my case to the extent I could,
and you could conclude with the judgement you find appropriate.
Yes, I do think it is redefinition of immutability on your terms, because here's what the python definition from the official docs:
http://docs.python.org/2/reference/datamodel.html
"""
The value of some objects can change. Objects whose value can change are said to be mutable; objects whose value is unchangeable once they are created are called immutable. (The value of an immutable container object that contains a reference to a mutable object can change when the latter’s value is changed; however the container is still considered immutable, because the collection of objects it contains cannot be changed. So, immutability is not strictly the same as having an unchangeable value, it is more subtle.) An object’s mutability is determined by its type; for instance, numbers, strings and tuples are immutable, while dictionaries and lists are mutable.
"""
By that definition, instances of these class are immutable:>>> class Age(int):
>>> class T(tuple):
... pass
...
>>> t = T(range(5))
>>> t[0] = 42
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'T' object does not support item assignment
... pass
...
>>> a = Age(10)
>>> id(a)
140374901745352
>>> a
10
>>> a += 20
>>> id(a)
6579456
>>> class Only42(object):
... @property
... def value(self):
... return 42
...
>>> o = Only42()
>>> o.value
42
>>> o.value = 10AttributeError: can't set attribute
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
>>>
If you want to extend that further and say that new attributes too should not be possible, use __slots__. Not sure if you read through the code I posted in my previous reply, so here goes ...
>>> class Only42(object):
... __slots__ = ['value']
... @property
... def value(self):
... return 42
...
>>> o = Only42()
>>> o.value = 10AttributeError: can't set attribute
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
>>> o.new_value = 20AttributeError: 'Only42' object has no attribute 'new_value'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
>>>
So, the point I was trying to make is 'mutable' and 'immutable' means something very specific in python. If you want your classes to behave like immutables types in python, derive from them (again, look at my code from the previous post) or use facilities like __slots__.
cheers,
- steve
Holy Stack Overflow, Batman, what have I done?!
Holy Stack Overflow, Batman, what have I done?!
--
You received this message because you are subscribed to the Google Groups "Python Pune" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pythonpune+...@googlegroups.com.
To post to this group, send email to pytho...@googlegroups.com.
Visit this group at http://groups.google.com/group/pythonpune?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
The original Only42 had no member attributes for the methods save the method itself.The correct counter assuming attribute which are bound to values should've been as followsclass Only(object):def __init__(self, value) :self._value = value@propertydef value(self):return self._valueonly42 = Only(42)setattr(only42, "_value", 43)print(only42.value)The way to programmatically enforce immutability is as follows :def __init__(self, value) :self._value = value@propertydef value(self):return self._valuedef __setattr__(self,name,val) :raise Exception("I intend this object to have its value perpetually set to {}".format(self._value))only42 = Only(42)setattr(only42, "_value", 43)print(only42.value)The way I simulate what a language support would've done is simulated through a @immutable decorator heredef immutable (cls):def blocksetattr(self,name,val) :raise Exception("I intend this object to be immutable")cls.__setattr__ = blocksetattrreturn cls@immutableclass Only(object):def __init__(self, value) :self._value = value@propertydef value(self):return self._valueonly42 = Only(42)setattr(only42, "_value", 43)print(only42.value)(gmail tells me you've sent me a response to my earlier reply .. but haven't seen it yet as I write and send this)
The original Only42 had no member attributes for the methods save the method itself.The correct counter assuming attribute which are bound to values should've been as follows
I'm being picky since want to ensure my casual style of writing / typos do not get interpreted differently from what I in my mind intended)
The original Only42 had no member attributes for the "class"/"object" save the method itself.The correct counter in a scenario where member attribute names are bound to values which are not methods would've been
A slightly more useful version of the immutable decorator (the earlier was indicative but crapped out in the __init__ itself)
def immutable (cls):def blocksetattr(self,name,val) :raise Exception("I intend this object to be immutable")oldinit = cls.__init__def replacement_init(*args, **kwargs) :oldinit(*args, **kwargs)cls.__setattr__ = blocksetattrcls.__init__ = replacement_initreturn cls@immutableclass Only(object):def __init__(self, value) :# this will get allowedself._value = value@propertydef value(self):return self._valueonly42 = Only(42)# This will failsetattr(only42, "_value", 43)print(only42.value)
- d
Holy Stack Overflow, Batman, what have I done?!
>>> def setme(self, name, val): self.__dict__[name] = val
...
>>> Only.__setattr__ = setme
>>> setattr(only42, "_value", 43)
>>> only42.value
43
did you assume __setattr__ be different than any other method ?
In my head at least, immutability in python is something either immutable types have (like numbers, strings and tuples) or something that you lay down as a contract using the facilities of the language. Breaking the contract (like perhaps what Noufal's colleague did in 'proving' strings to be immutable) doesn't count as valid behaviour.
If for you, immutability implies that assignment (or modification of values via any other approach) is disallowed, no matter what, yes, python doesn't have immutable objects, just immutable types -- which also aren't really immutable because you can hack the C-representation. This though, afaict, is not the python definition of immutability.
cheers,
- steve
--
You received this message because you are subscribed to the Google Groups "Python Pune" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pythonpune+unsubscribe@googlegroups.com.
To post to this group, send email to pytho...@googlegroups.com.
Visit this group at http://groups.google.com/group/pythonpune?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.