Andrey Bushman
Andrey Bushman

Reputation: 12516

Why Python hasn't true constants? Is it not dangerous?

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?

Upvotes: 15

Views: 849

Answers (3)

sahama
sahama

Reputation: 679

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.

Upvotes: 2

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477804

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 .

Upvotes: 11

Dunno
Dunno

Reputation: 3684

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.

Upvotes: 3

Related Questions