Re: Read-only attribute in module
Terry Reedy wrote On 2/10/2012 6:11 AM, mloskot wrote: The intent of xyz.flag is that it is a value set by the module internally. xyz is a module wrapping a C library. The C library defines concept of a global flag set by the C functions at some events, so user can check value of this flag. I can provide access to it with function: xyz.get_flag() If the value of the flag can change during a run, I would do that. Otherwise, you have to make sure the local copy keeps in sync. Users might also think that it is a true constant that they could read once. I understand that you might be concerned that one person in a multi-programmer project might decide to rebind xyz.flag and mess up everyone else. I think the real solution might be an option to freeze an entire module. Terry, Thanks for your really helpful notes. Best regards, - -- Mateusz Loskot http://mateusz.loskot.net -- View this message in context: http://python.6.n6.nabble.com/Read-only-attribute-in-module-tp4378950p4464760.html Sent from the Python - python-list mailing list archive at Nabble.com. -- http://mail.python.org/mailman/listinfo/python-list
Re: Read-only attribute in module
On 10 February 2012 03:27, Terry Reedy tjre...@udel.edu wrote: On 2/9/2012 8:04 PM, Steven D'Aprano wrote: Python happily violates consenting adults all over the place. We have properties, which can easily create read-only and write-once attributes. So propose that propery() work at module level, for module attributes, as well as for class attributes. I think Steven would like something else: bare names that cannot be rebound. E.g. something like: const a = 42 a = 7 Would raise an exception. Is that right? -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list
Re: Read-only attribute in module
Terry Reedy wrote On 2/9/2012 6:43 AM, Mateusz Loskot wrote: import xyz print(xyz.flag) # OK xyz.flag = 0 # error due to no write access Why prevent that? If you called it 'FLAG', that would indicate that it is a constant that should not be changed. While Python make some effort to prevent bugs, it is generally a 'consenting adults' language. Terry, The intent of xyz.flag is that it is a value set by the module internally. xyz is a module wrapping a C library. The C library defines concept of a global flag set by the C functions at some events, so user can check value of this flag. I can provide access to it with function: xyz.get_flag() But, I thought it would be more convenient to have a read-only property in scope of the module. Sometimes, especially when wrapping C code, it is not possible to map C API semantics to Python concepts as 1:1. - -- Mateusz Loskot http://mateusz.loskot.net -- View this message in context: http://python.6.n6.nabble.com/Read-only-attribute-in-module-tp4378950p4382967.html Sent from the Python - python-list mailing list archive at Nabble.com. -- http://mail.python.org/mailman/listinfo/python-list
Re: Read-only attribute in module
On Thu, 09 Feb 2012 22:27:50 -0500, Terry Reedy wrote: On 2/9/2012 8:04 PM, Steven D'Aprano wrote: Python happily violates consenting adults all over the place. We have properties, which can easily create read-only and write-once attributes. So propose that propery() work at module level, for module attributes, as well as for class attributes. I'm not wedded to a specific implementation. Besides, it's not just a matter of saying property should work in modules -- that would require the entire descriptor protocol work for module lookups, and I don't know how big a can of worms that is. Constant names is a lot more constrained than computed name lookups. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Read-only attribute in module
On 2/10/2012 6:11 AM, mloskot wrote: The intent of xyz.flag is that it is a value set by the module internally. xyz is a module wrapping a C library. The C library defines concept of a global flag set by the C functions at some events, so user can check value of this flag. I can provide access to it with function: xyz.get_flag() If the value of the flag can change during a run, I would do that. Otherwise, you have to make sure the local copy keeps in sync. Users might also think that it is a true constant that they could read once. I understand that you might be concerned that one person in a multi-programmer project might decide to rebind xyz.flag and mess up everyone else. I think the real solution might be an option to freeze an entire module. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Read-only attribute in module
Hi, I'm implementing Python 3 extension using the Python C API. I am familiar with defining new types, implementing get/set for attributes, etc. I'm wondering, is there any mean to implement attribute in module scope which is read-only? So, the following import xyz print(xyz.flag) # OK xyz.flag = 0# error due to no write access Best regards, -- Mateusz Loskot, http://mateusz.loskot.net -- http://mail.python.org/mailman/listinfo/python-list
Re: Read-only attribute in module
Mateusz Loskot mate...@loskot.net writes: I'm wondering, is there any mean to implement attribute in module scope which is read-only? Python is designed by and for consenting adults. Rather than restricting, instead use conventions to make your intent clear to the user of your library. So, the following import xyz print(xyz.flag) # OK xyz.flag = 0# error due to no write access PEP 8 URL:http://www.python.org/dev/peps/pep-0008/ gives the style guide for Python code (strictly for the standard library, but it is recommended for all Python code). If you want a module attribute that is intended to remain bound to the same value, use PEP 8's recommendation and name the attribute in ‘ALL_UPPER_CASE’. If you want an attribute that is intended only for internal use (an implementation detail that should not be relied upon outside the library), use PEP 8's recommendation and name the attribute with a ‘_single_leading_underscore’. -- \ “We jealously reserve the right to be mistaken in our view of | `\ what exists, given that theories often change under pressure | _o__) from further investigation.” —Thomas W. Clark, 2009 | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Re: Read-only attribute in module
Ben Finney-10 wrote Mateusz Loskot lt;mateusz@gt; writes: So, the following import xyz print(xyz.flag) # OK xyz.flag = 0# error due to no write access PEP 8 lt;URL:http://www.python.org/dev/peps/pep-0008/gt; gives the style guide for Python code (strictly for the standard library, but it is recommended for all Python code). Ben, That's what I thought really. Thank you for confirming the sanity of the style-powered conventions. Best regards, - -- Mateusz Loskot http://mateusz.loskot.net -- View this message in context: http://python.6.n6.nabble.com/Read-only-attribute-in-module-tp4378950p4380150.html Sent from the Python - python-list mailing list archive at Nabble.com. -- http://mail.python.org/mailman/listinfo/python-list
Re: Read-only attribute in module
On 2/9/2012 6:43 AM, Mateusz Loskot wrote: Hi, I'm implementing Python 3 extension using the Python C API. I am familiar with defining new types, implementing get/set for attributes, etc. I'm wondering, is there any mean to implement attribute in module scope which is read-only? Not that I know of. Python intentionally leaves modules mutable even after the code has executed so they can be monkey-patched from outside. The argument for is that it is unusual to do so but sometimes very useful and necessary. The main argument against is that it prevents optimizations that would make code in modules run faster. import xyz print(xyz.flag) # OK xyz.flag = 0 # error due to no write access Why prevent that? If you called it 'FLAG', that would indicate that it is a constant that should not be changed. While Python make some effort to prevent bugs, it is generally a 'consenting adults' language. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Read-only attribute in module
On 09/02/2012 11:43, Mateusz Loskot wrote: Hi, I'm implementing Python 3 extension using the Python C API. I am familiar with defining new types, implementing get/set for attributes, etc. I'm wondering, is there any mean to implement attribute in module scope which is read-only? So, the following import xyz print(xyz.flag) # OK xyz.flag = 0# error due to no write access Best regards, There's a recipe by Alex Martelli here http://code.activestate.com/recipes/65207-constants-in-python/ but most people wouldn't bother with it. As others have said simply use THIS_IS_A_CONSTANT. -- Cheers. Mark Lawrence. -- http://mail.python.org/mailman/listinfo/python-list
Re: Read-only attribute in module
On Thu, 09 Feb 2012 23:32:59 +1100, Ben Finney wrote: Mateusz Loskot mate...@loskot.net writes: I'm wondering, is there any mean to implement attribute in module scope which is read-only? Python is designed by and for consenting adults. Rather than restricting, instead use conventions to make your intent clear to the user of your library. Oh I agree. The convention I want to use to make my intent clear is the same convention used for the rest of Python: a runtime exception. I find this consenting adults argument less than convincing. We already have constants in Python -- every int and float and string is a constant. You can't modify the object 1 to have the value 42, and for very good reason, consenting adults be damned. What we don't have is *named* constants. Python has no shortage of read-only values and members, e.g.: class A(object): ... pass ... A.__dict__ = {} Traceback (most recent call last): File stdin, line 1, in module AttributeError: attribute '__dict__' of 'type' objects is not writable Consider this: If you pass an int to len(), you get a runtime error telling you that you did something wrong. Does this violate consenting adults? No, if you want to, you can monkey-patch len to accept ints, but you have to work at it. The normal behaviour is to get an error, not some arbitrary but meaningless behaviour. You certainly don't get told to use a naming convention (Hungarian notation, perhaps) to avoid passing the wrong value to len(). If you assign to something which is intended to be constant, you don't get an exception telling you that you made a mistake. Instead, you get unspecified (but almost certainly incorrect) behaviour in the module, and told to use a naming convention to remind you not to screw up. The excuse given is that Python is for consenting adults, but I don't believe it. I believe that the real reason is that it is hard to introduce named constants to Python, and rather than solve that hard problem, people just whitewash the issue and pretend that it's a feature. It's not a feature, it's a wart. There is no conceivable use-case for allowing math.pi = 2.5 to succeed. Python happily violates consenting adults all over the place. We have properties, which can easily create read-only and write-once attributes. We have descriptors which can be used for the same. We have immutable types, and constant values, but not constant names. Python can enforce all common software contracts I can think of, except the contract that a name will be set to a specific value. And that is, in my opinion, a weakness in Python. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Read-only attribute in module
On 2/9/2012 8:04 PM, Steven D'Aprano wrote: Python happily violates consenting adults all over the place. We have properties, which can easily create read-only and write-once attributes. So propose that propery() work at module level, for module attributes, as well as for class attributes. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list