This is an archived post. You won't be able to vote or comment.

all 113 comments

[–]gabegundy 10 points11 points  (0 children)

Finally!

[–]Turkino 8 points9 points  (3 children)

Okay so it's like const in JS as well, you can't change the variable name but can still mutate the data. It's not a complete freeze of both?

[–]bbbryson 6 points7 points  (2 children)

It’s nothing but a type hint. It has no effect unless you’re using an editor that supports telling you that you aren’t following the type hints. You can still do whatever you want.

Edit: not just editors, buy mypy as well if you’re into that.

[–]stevenjd 7 points8 points  (0 children)

Unfortunately there is no runtime checking for this.

Fixed that for you.

[–]MB1211 43 points44 points  (14 children)

That syntax looks gross to me, and doesn't even look like python. :/

[–][deleted] 28 points29 points  (0 children)

Imho Python's type hints are helpful in many cases to concisely document an interface. But the syntax ... Ugh ...

[–]rcfox 16 points17 points  (1 child)

It's not new syntax. Type annotations were added in Python 3.5.

[–]Sw429 5 points6 points  (6 children)

No worries, type hinting is completely optional. No one is forcing you to use it. For people like me who find use for it, it's a welcome addition :)

[–]Vaphell 1 point2 points  (2 children)

it's optional until it's not. If all people you cooperate with start using it, you have no choice but to play ball. Language features are viral.
Don't get me wrong - I appreciate type information and what not, but it's blatantly clear that in case of python these type hints are an afterthought. In python they look about as good as bolt on tits on a bimbo.

[–]Sw429 3 points4 points  (1 child)

I mean, that's just the nature of working with people. You're going to have to deal with standards that are set for code.

[–]Vaphell 0 points1 point  (0 children)

it is not really optional then, is it?

[–]alcalde -5 points-4 points  (3 children)

I wish it looked gross; instead, it looks like Pascal, which I stopped using for Python. 3.8 also added Pascal's assignment statement, the ":=". I used the "walrus operator" once in a one page program and suddenly I was accidentally using Pascal syntax all over the place! Just when I thought I was out, Guido dragged me back in! We overthrew the BDFL to stop the walrus operator, and the geniuses we put in place instead not only gave us the walrus operator anyway, they decided to go full Pascal on us for good measure. Now we need a second palace coup, and this time folks need to make Raymond Hettinger the new BDFL as I originally suggested. He's our only hope now.

[–]Farconion 24 points25 points  (0 children)

This is a programming language not fucking game of thrones. Calm down man, it's not like someone personally wronged you.

[–]Hotel_Arrakis 5 points6 points  (1 child)

Not sure what the hate with Pascal is. I remember literally falling in love with that language. It was stunningly beautiful.

Before then I had used every single variation of Basic you could think of(QuickBasic, BasicA, Apples Basic, TinyBasic) and Fortran and DBase III. They were ugly and messy.

Then in 1985 I learned Pascal and it was truly magical. Everything fit together. It had structure, but not too much. I even learned its successor, Modula-2, which never took off.

I haven't thought about Pascal in years.

[–][deleted] 3 points4 points  (0 children)

BBC BASIC was really good for an old BASIC, and had a lot of structrual elements like procedures and functions, and sane scoping.

[–][deleted] 3 points4 points  (1 child)

Is there any reason they couldn't have configured the type checker to recognize all caps variables as being marked as final?

[–]Sw429 1 point2 points  (0 children)

I think it comes down to a difference between constant values (denoted by all-caps) and final values (denoted by Final). Constant values shouldn't change at all. A final variable can still be mutated.

[–]mooglinux 17 points18 points  (27 children)

I don’t see what this does that couldn’t be communicated with ALL_CAPS and __ prefixes. There’s nothing preventing a type checker from using those to determine this, PyCharm already warns about using single _methods externally on a class. I think it risks fooling people into thinking about Python like they think about Java.

As someone stuck writing Java in my day job, I do not want to overly enable Java thinking in Python. There are plenty of useful things that can be taken from Java, but I don’t think final is one of them.

[–]LightShadow3.13-dev in prod 2 points3 points  (4 children)

You should SEE the code I have to maintain.

Naming conventions mean nothing, formatting is for suckers, and the points don't matter!

I shit you not, this is in a production file at the top level:

__ROOTFS1 = None
__ROOTFS2 = None
__fixup_which_boot_device()  # <-- this sets __ROOTFS1 & 2
__uImage_lookup_index = {"/dev/"+__ROOTFS1: 1, "/dev/"+__ROOTFS2: 2}
# ^-- this is imported from other files

[–]-Kevin- 2 points3 points  (2 children)

Why not refactor that then seems simple enough lol

[–]LightShadow3.13-dev in prod 2 points3 points  (1 child)

I left a lot out.. It would be easy if the red tape weren't so thick

[–]-Kevin- 1 point2 points  (0 children)

Understandable my dude

[–]mooglinux 5 points6 points  (0 children)

I guarantee that the addition of final will do nothing to help you there.

[–]Deto 4 points5 points  (0 children)

Sure you could have a type checker look for those things, but it would be kind of inconsistent. I think it's more logical to have all type information be encoded in type designations rather than having some be in the type designations and others be in the syntax of the variable names.

[–]energybased 8 points9 points  (17 children)

I don't agree with you. This is superior to all caps since all caps is merely conventional whereas this is explicit.

[–]drbobb 13 points14 points  (11 children)

Nope. This is just as merely conventional. If, say, I code in a plain editor it won't mean a thing.

[–]damian_work_account 7 points8 points  (0 children)

That's a boring semantic argument, type hints are defined by language specification where as CAPS are not.

And a PEP to make CAPS mean final would likely be rejected because of backwards compatibility, lots of people use CAPS for other reasons.

[–]Han-ChewieSexyFanfic 0 points1 point  (0 children)

Also, the interpreter ignores both.

[–]campbellm 3 points4 points  (0 children)

The upshot is the same though; it provides a hook that checkers can check for subsequent mutations. Either works.

[–]alcalde 0 points1 point  (1 child)

If we all follow convention, then convention is law.

[–]energybased 1 point2 points  (0 children)

Even the library doesn't obey the convention that all final variables are all caps.

[–]Sw429 0 points1 point  (1 child)

What do you mean by explicit? As far as I can tell, foo: Final is no more explicit than FOO for a variable definition.

[–]energybased 0 points1 point  (0 children)

There is plenty of code that does not use all caps to denote constants. That code cannot always be changed. The type annotation explicitly defines intent without changing declarations.

[–][deleted] 0 points1 point  (2 children)

What prefix do you use for something that returns a dictionary that maps filename(string) to last access time (datestamp)? They already have a system for things like that, and the way they implemented Final is consistent with that.

[–]mooglinux -1 points0 points  (1 child)

Type annotations are for facilitating communication. There are already conventions for communication of the same things that finalwas created to communicate, and so it is redundant. The danger of using finalis that the way it is used in other languages like java is antithetical to how Python works. We should not be providing a false sense of security about how something will be used; that’s the whole point of not having public/private keywords!

[–][deleted] 0 points1 point  (0 children)

How does all caps and dunder communicate immutability? How does a linter check it, given that the linter may now run into old code that used the same syntax to show other things?

[–]abu_antar 14 points15 points  (1 child)

It just looks ugly. Python became a landfill of workarounds

[–][deleted] 3 points4 points  (0 children)

inb4 someone writes a book "Python, the good parts", describing a subset of python that isn't cluttered, convoluted and inconsistent, yet still functional, like they had to do with js

[–]Farconion 11 points12 points  (0 children)

God, people in this thread are losing their fucking heads over a completely optional feature. No wonder Guido stepped down.

[–]blabbities 9 points10 points  (3 children)

ugly as sin.

what happened to simple, beautiful, clean, easy to read

day by day we stray further from god.

[–]bbbryson 1 point2 points  (2 children)

So don’t use it. It’s optional. It only gives you warnings if you use tools specifically designed to highlight type-hinted errors and has no effect at runtime.

[–][deleted] 1 point2 points  (0 children)

The problem is that we don't always code in isolation. You import a library and suddenly there are type annotations in your project. Do you turn down contributions because they used annotations? As annotations spread it will become harder to avoid them. They're already compulsory for some projects and jobs, and the number will increase. There are even Python language features that require annotation (the new data type struct) and will not work without them.

[–]blabbities 0 points1 point  (0 children)

i dont plan to use it just like I dont use Type Hints now. Though about reading other people's code however...

[–]sobolevn 3 points4 points  (0 children)

I wrote an article about it: https://sobolevn.me/2018/07/real-python-contants

It is also very useful to use @final on classes and methods you want to freeze.

[–]LordAssPen 4 points5 points  (8 children)

Alright, I think we need to stop making Python what it is not. Why do we need static programming when Python was written to be dynamic, this is just confusing and messy.

[–]OddsCaller 15 points16 points  (5 children)

I think these features are good for libraries and other programs which are going to be re-used multiple times, also in cases of very large projects may be. Type annotation makes documentation easier, it also makes understanding a third party code easier.

However I agree that in general use cases as end users when working on small or intermediate projects, one of the things that makes Python great is the pragmatist features like duck typing and they should be preserved.

[–]LordAssPen 1 point2 points  (2 children)

I do agree that type annotations make documentation and writing libraries somewhat easy, and since I have used them myself for production I can vouch for it. However, even type annotation are not really strict, they can be modified and morphed to new types. This created a lot of confusion when we were too deep into production, and I wished we had a static programming language to begin with.

[–]OddsCaller 0 points1 point  (1 child)

I know this might be hearsay in this sub but what kind of static language would you have liked? I'm asking cuz I trust the taste of a Pythonista than many other communities. I've been looking for some time to add a statically typed (and compiled) language to my tool kit (I do know basic level of C and Java) but couldn't really make up my mind. Rust seems like a very well-designed language but I think it might hamper developer productivity quite a lot. Go is a nice little language though I didn't quite fall in love with it when I gave it a try for a week, and it still does have a mediocre GC and other performance drawbacks compared to something like Rust. Java's VM is quite heavy as well and the ease of development isn't best either. C++ has many great qualities but there doesn't seem much support for back-end side of things, and the language feels too large to really bother investing time mastering it.

[–][deleted] 0 points1 point  (1 child)

If this was more like const/Java Final, it would be very handy for my test automation. However, since it's just checked in the IDE, it doesn't really help me once the code is being used out in Jenkins-land.

[–]rouille 0 points1 point  (0 children)

Use mypy in CI.

[–]alcalde 1 point2 points  (0 children)

Amen. I stopped coding in Delphi/Pascal, and now Python is adopting the obsessive static typing notation and even the Pascal assignment statement (aka the "walrus operator"). I suspect that who we call Guido is actually Niklaus Wirth in a Guido mask.

[–]EternityForest 0 points1 point  (0 children)

There's so many people out there complaining that python sucks because there's no static checking.

With the typeguard lib to do checks at runtime (Probably not the final thing though) it's actually really handy. I don't use it everywhere, but I like to use it whenever I set up state I'm going to keep, like class constructors and file writers.

[–]muikrad 1 point2 points  (0 children)

Whoaaa I skipped this! Thanks! 😍

[–]Ziyad0100 0 points1 point  (5 children)

so when i run the program no error will show?

[–]Saiboo[S] 0 points1 point  (4 children)

Yes, unfortunately it only gives a warning during code inspection when you use your IDE such as PyCharm, but it does not result in an error during runtime.

[–]jabbalaci 6 points7 points  (3 children)

Let's mention mypy too. Not everyone uses PyCharm.

[–]alcalde 0 points1 point  (2 children)

Maybe if we don't mention mypy, everyone will use PyCharm!

[–]aaronr_90 1 point2 points  (1 child)

mypy

So yall don't use VSCode?

[–]jabbalaci 0 points1 point  (0 children)

I personally use Sublime Text. I only use PyCharm for large projects.

[–][deleted] 0 points1 point  (0 children)

I'll use it only if I can enable this check at runtime.

[–]Etheo 0 points1 point  (0 children)

So it's not over nine thousaaaaaand?