15

Python 3

I learn Python via a book. Right now I learned that Python hasn't constants in that sense in which they are available in C ++ or C#... For example, it is possible to write such danger code:

>>> import math
>>> math.pi
3.141592653589793
>>> math.pi = 123.456
>>> math.pi
123.456
>>> 

Hm... It is an unpleasant surprise for me...

I.e. potentially I am not protected from the fact that any module (which is not mine) which I load into my code, can damage data which shan't change.

Why in Python it is so made? Is it not dangerous?

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
Andrey Bushman
  • 11,712
  • 17
  • 87
  • 182
  • 3
    I think it is one of the design principles of Python that it is made "unsafe" on purpose. This is useful if you want to patch code: now and then I find code with a minor error, and I can simply overwrite the function with my own one. – Willem Van Onsem Jan 16 '17 at 17:32
  • 2
    Because Python is a dynamic language. Almost *everything* can be replaced at runtime. Given that there are huge codebases using Python, the answer is: nope. – Martijn Pieters Jan 16 '17 at 17:32
  • Because `const` is for weak programmers – Dunno Jan 16 '17 at 17:34
  • @WillemVanOnsem, But at this case there is no warranty that someone else's module which you load won't 'correct' your code. – Andrey Bushman Jan 16 '17 at 17:34
  • 1
    @AndreyBushman: there are in fact a few ways to protect classes and objects (making them immutable), but these are almost never used. In Python you indeed have to trust to some extent that module programmers do not intend to do bad things. – Willem Van Onsem Jan 16 '17 at 17:35
  • 1
    Isn't your concern about loading a module ameliorated by namespace? just don't use `from module import *` and you should be fine. – juanpa.arrivillaga Jan 16 '17 at 17:40
  • 1
    https://xkcd.com/1428/ – C S Jan 16 '17 at 17:41
  • 1
    "Is it not dangerous?" Almost all languages have dangerous features, which are often the flip-side of powerful features. The handful which in some sense don't (such as certain functional languages) are comparatively little used. – John Coleman Jan 16 '17 at 17:42
  • 2
    [Python trusts you. It says "hey, if you want to go poking around in dark places, I'm gonna trust that you've got a good reason and you're not making trouble." After all, we're all consenting adults here.](https://mail.python.org/pipermail/tutor/2003-October/025932.html) – Alden Jan 16 '17 at 17:47
  • 1
    C and C++ don't have any of the protection you think they have, either. Untrusted or buggy code can overwrite a `const` in C or C++ and get all sorts of undefined behavior nastiness. Sure, it'll probably have to cast away the `const` somewhere, but if you're loading untrusted code, casting away `const` is the least of what that code can do. – user2357112 Jan 16 '17 at 20:04
  • @MartijnPieters Is the fact that there are big codebases in Python really a good proof for that the lack of constants isn't causing trouble? – Alex Jan 16 '17 at 21:32
  • @Alex: put it this way: if this really was a big issue, the language would not see the adoption it has, or constants would have been added. – Martijn Pieters Jan 16 '17 at 21:35
  • @MartijnPieters I'm not sure adaption of a language is strongly correlated with the quality of the language itself. Last time I checked, even though that was a handful of years ago, PHP was the undisputed master of the web with like 70-90% of the sites. And there are undoubtedly huge systems built in PHP, probably more and bigger than Python. Does that mean that PHP is/was the best language for the web? Or that PHP does not have any big issues? – Alex Jan 16 '17 at 22:52
  • @MartijnPieters Not saying that the lack of constants is a significant problem in Pyton, I'm just skeptical to using it's adoption (quite moderate compared to other big languages?) as a proof that it's not. – Alex Jan 16 '17 at 22:54
  • @Alex touché; but then again people do try and fix the flaws; see http://hacklang.org/. Same for JavaScript; the advances in the language these past few years are all due to professional developers trying to iron out the considerable wrinkles in the language. – Martijn Pieters Jan 16 '17 at 23:30

3 Answers3

11

Python is a dynamic language: you can add/remove/modify attributes and functions of objects such that they suite your need. Although this is a bit bold, basically an object is no more than a dictionary. You can simply add attributes to one object, etc. Python also supports - more or less - the idea of duck typing:

"If it looks like a duck and quacks like a duck, it's a duck".

In other words, if you want to let an object look like another object a certain function expects, simply give it feathers and legs and remove its second head and the function will accept it.

This is one of the design principles of Python and it can be very useful: say you want to patch a function in a piece of code you can simply:

def better_function(param,eter):
    #...
    pass

somemodule.badfunction = better_function

and thus fix the code. Furthermore if there is a package that for instance does some nice things with webpages, you can easily modify it to do things with say printers, by rewriting a few methods.

The downside is indeed that you have less control about modules that would introduce bugs (on purpose; I am not claiming they do, although one cannot exclude malicious intent). There are some ways to protect your code, for instance making classes and objects immutable: you can overwrite the __setattr__ and __delattr__ functions that are called to set and delete an attribute and thus make it hard/impossible to alter an object, but this would destroy the dynamism of Python: Python programmers expect they can alter an object.

Heavy protection of objects/classes/... is however against the principles of a dynamic language. If you want a language that guarantees such things, you better use a statically typed language like .

Community
  • 1
  • 1
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
3

Python is kind of a special programming language, because it strongly relies on your work hygiene rather than on build-in mechanisms. If you want to maintain a large python project, you can't do without unit tests, code review etc, but then again, you need that in every other technology.

Once you have code review, you don't really need consts, because even the most inexperienced reviewers will notice that you're changing math.pi or something named IN_THIS_CONVENTION and that maybe it's not a good idea.

Dunno
  • 3,632
  • 3
  • 28
  • 43
  • 1
    Some applications allow to create Add-Ins for them on Python. Such extensions can be placed in web. Users can download and use it. It means that such scripts also can be written with danger bugs or even harmful code which is intended to intentionally enter errors into computation process,through temporarily damaging constants. – Andrey Bushman Jan 16 '17 at 17:55
  • Python is not special in this way, there are [plenty more](https://en.wikipedia.org/wiki/Dynamic_programming_language#Examples). – Martijn Pieters Jan 16 '17 at 21:36
2

In addition.

for preventing mistakes you can use code like this in class

class C:
    def __init__(self):
        self.__a = 0

    @property
    def a(self):
        return self.__a

    @a.setter
    def a(self, new_value):
        raise Exception('YOU CAN NOT MODIFY A')

c = C()
# print(c.__a) # can not access to __a
print(c.a)
# c.a = 2 # can not set new value for __a via c.a

but in this form also you can modify __a via

c._C__a = 2
print(c.a)

i think this is enough to prevent mistakes.

sahama
  • 669
  • 8
  • 16