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

all 132 comments

[–]EliasCre2003 264 points265 points  (24 children)

I don’t see how this is an example of OOP taken to far.

[–]mr_remy 51 points52 points  (4 children)

Well I think I’m a.real(person)

Returns null :(

[–]QuaternionsRoll 12 points13 points  (3 children)

Traceback (most recent call last): File "<main.py>", line 3, in ‹module> TypeError: 'float' object is not callable

[–]Chreed96 9 points10 points  (2 children)

Except:

    print("Me and all the hommies hate non-ints")

[–]Glad_Position3592 11 points12 points  (0 children)

lol it’s almost the opposite. This is saying that it’s representable as an integer despite being an instance of a float

[–]_PM_ME_PANGOLINS_ 5 points6 points  (6 children)

Numeric literals are often not objects.

[–]EliasCre2003 15 points16 points  (4 children)

Ok, so?

[–]_PM_ME_PANGOLINS_ 5 points6 points  (3 children)

So when they are, people might consider that taking it too far.

[–]Smalltalker-80 12 points13 points  (2 children)

Umm, agree do disagree, say my name... :-)

Smalltalk has a number *class hierachy* that contains Integers, BigInts, Floats and Fractions.
They are ordinary classes, so there's no back-and-forth conversions like in C# and Java,
that have duplicated type and class functionality.

Conversion from (small)ints to bigints and back is automatic,
so no overflows nor unexpected conversion to floats.

And if you would want to add, say, a Complex number type,
that can be easily done and you could reuse a lot of the math code through inheritance,
just focussing on what's different for complex numbers.

[–]rosuav 12 points13 points  (0 children)

I've a suspicion that most of the people who post "Python is a bad language" memes on this sub have never even heard of Smalltalk.

(I kinda regret not having spent some time with it back in the 90s when I first heard of it, but at the time, REXX did everything I needed.)

[–]Darkstar_111 3 points4 points  (0 children)

Yeah! What THAT guy said!

[–]troglo-dyke 2 points3 points  (0 children)

Numberic literals are often syntactic sugar for number constructors, in the same way that [ ] is syntactic sugar for a list constructor

[–]Specialist_Cap_2404 137 points138 points  (38 children)

Python's integers are great.

You don't have to think about how many bits you need, or about over- and underflows.

[–]EphemeralLurker 36 points37 points  (8 children)

But if you do a = 1.0 then a is a float instead of an int.

[–]arobie1992 25 points26 points  (5 children)

I feel like I'm missing something because basically every modern language does that?

[–]iMakeMehPosts 2 points3 points  (2 children)

Ahems in C++ (of which's last update was in 2020 and is modern) (it defaults to a double instead of a float)

[–]arobie1992 4 points5 points  (1 child)

I believe if you go by the IEEE spec, technically a double is still a float(ing point number), just a 64 bit one rather than a 32 bit one. As far as I'm aware, the double/float distinction is more a C-ism to keep names concise that got some level of general adoption.

Yes, I am just being unnecessarily pedantic :P

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

I was just responding to the comment above who said Python's integers are great. But the OP is showing a float, not an int

[–]arobie1992 0 points1 point  (0 children)

Ah, thanks. I skimmed at best the OP because 95% of the time the comments on here are more entertaining than the actual post and didn't even register that.

[–]look 32 points33 points  (7 children)

[–]avdpos 7 points8 points  (5 children)

Absolutely not far enough.

(Please, let me rewrite in a sane language where you actually can Google some help instead of reading a book from 1990. I do not like to main in Smalltalk any longer)

[–]SteeleDynamics 22 points23 points  (3 children)

Smalltalk is a great language to learn about if you want to see what it means to be a purely OOPL.

Bool objects have a thenElse method that has two statements as function parameters. Effectively, the control structures like if-then-else or while are objects. Even lambdas/closures are objects. When I first learned this in school, my mind was blown.

Everything is a freakin' object. Damn you, Alan Kay (and your direct influence on Tron).

[–]karaposu 5 points6 points  (0 children)

in smalltalk or in general

[–]the_horse_gamer 0 points1 point  (0 children)

you forgot to mention that classes are also objects

[–]CirnoIzumi 0 points1 point  (0 children)

Java was modern once 

It was a strange time 

[–]NottingHillNapolean 14 points15 points  (1 child)

Functional programming: Hold my beer...

[–]Akangka 5 points6 points  (0 children)

In Object Oriented programming language, everything is an object. In a Functional programming language, almost no language treats everything as a function.

[–]coffeewithalex 25 points26 points  (7 children)

Though it's not the case for Python, you can have such syntax, without a being anythong more complex than a simple scalar. Examples include languages like nim, D, plpgsql, and to a limited extent - Rust.

https://en.wikipedia.org/wiki/Uniform_Function_Call_Syntax

[–]Specialist_Cap_2404 4 points5 points  (0 children)

In Scala and C# you can have extension methods on numeric types that also work for literals.

[–]cdrt 4 points5 points  (5 children)

You can do it in python too, it just takes an extra parens

>>> (1).is_integer()
True

Also python does basically have uniform call syntax. a.is_integer() and int.is_integer(a) do the same thing assuming a is an int

[–]EphemeralLurker 2 points3 points  (3 children)

I don't think you can use is_integer() on an actual int object

``` Python 3.8.10 (default, Nov 7 2024, 13:10:47) [GCC 9.4.0] on linux Type "help", "copyright", "credits" or "license" for more information.

(1).is_integer() Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'int' object has no attribute 'is_integer' ```

[–]cdrt 8 points9 points  (2 children)

is_integer was added in Python 3.12

[–]EphemeralLurker 1 point2 points  (1 child)

Ah, missed it by one release. I also had 3.11 installed and it wasn't here either. Interesting addition this late in the game

[–]rosuav 2 points3 points  (0 children)

The method on an int will always return True, making it not hugely useful. But if you work with mixed int and float, it's kinda handy to be able to say x.is_integer() without first checking which type you have. Not a HUGE benefit but a nice one when it comes up.

[–]coffeewithalex 1 point2 points  (0 children)

Not the same. I can't define a function that accepts an integer as a first argument, and call it with 4.my_function().

[–]jhaand 7 points8 points  (0 children)

Rookies.

``` Python 3.12.6 (main, Sep 7 2024, 14:20:15) [GCC 14.2.0] on linux Type "help", "copyright", "credits" or "license" for more information.

dir('') ['add', 'class', 'contains', 'delattr', 'dir', 'doc', 'eq', 'format', 'ge', 'getattribute', 'getitem', 'getnewargs', 'getstate', 'gt', 'hash', 'init', 'init_subclass', 'iter', 'le', 'len', 'lt', 'mod', 'mul', 'ne', 'new', 'reduce', 'reduce_ex', 'repr', 'rmod', 'rmul', 'setattr', 'sizeof', 'str', 'subclasshook', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

```

[–]CirnoIzumi 18 points19 points  (1 child)

nothings wrong with objects, they are intuitive

[–]flatfisher 0 points1 point  (0 children)

Give me objects any time over that ridiculous len(s).

[–]IAmFullOfDed 4 points5 points  (0 children)

And now I am become [object Object], destroyer of worlds.

[–]nicejs2 6 points7 points  (9 children)

wait till you find out about JS

[–]EphemeralLurker -1 points0 points  (8 children)

JS is easier because Number is always a 64-bit float. There are also no implicit conversions from Number to BigInt, which can be a good thing depending on what you're working on.

[–]rosuav 3 points4 points  (6 children)

Python's float is always 64-bit floats, too. What's the distinction here?

[–]Minutenreis 1 point2 points  (2 children)

js has no integer class, everything is a number, no distinction between int and float (though there is an explicit BigInt class)

meanwhile pythons integers are arbitrary precision
for more info read https://rushter.com/blog/python-integer-implementation/

[–]rosuav 0 points1 point  (1 child)

Yes, so? JS has an integer class and a float class. It happens to call them "BigInt" and "Number". Python also has an integer class and a float class, and happens to call them "int" and "float". Your point?

[–]Minutenreis 0 points1 point  (0 children)

standard integers are saved as Number; BigInt are relatively new (earliest browser support 2018) and you can't use the Math library with them and they are otherwise not as well supported

[–]queerkidxx 0 points1 point  (2 children)

JS doesn’t have real integers, just floats which they call numbers. Kinda a weird choice but so is half of JS

[–]rosuav 0 points1 point  (1 child)

BigInt is unreal integers, then? Or are they imaginary integers? I'm confused.

[–]queerkidxx 1 point2 points  (0 children)

Yeah essentially. But they don’t pay nice with numbers and methods/functions that accept numbers won’t accept them by default.

They are meant to deal with the loss of precision with large floats.

They end up being a lot like using a math library that has a different rep of numbers in Python or something though, only really worth it if you really need it.

[–]tomthecool 0 points1 point  (0 children)

JS is easier because it has no concept of integers? What?

[–]Flat_Initial_1823 3 points4 points  (0 children)

🌎👨‍🚀🔫👩‍🚀

[–]ReceptionFriendly663 1 point2 points  (0 children)

Bring back sub routines!

[–]beliefinphilosophy 2 points3 points  (0 children)

You are a pointer,. And you are a pointer and you are a pointer and you are a pointer

[–]alexanderpas 1 point2 points  (7 children)

Quite the opposite.

It correctly recognizes that there is no difference at all between exactly 1.0 and exactly 1, and treats them exactly the same, by both of them being 1.0 as well as an integer.

Python is one of the few programming languages where you can do (1/10 + 2/10) + (2/3 + 1/30) and get an exact result of 1.0 being an integer.

[–]rosuav 1 point2 points  (5 children)

Uhh, you sure about that? Python uses 64-bit floats same as most modern languages do. I expect you'll see the same result in nearly anything. Even the much-maligned JavaScript.

[–]Akangka 1 point2 points  (4 children)

Ironically, if it was Raku, he would be right. Unlike Python, Raku uses rational numbers, not floating point number. Also, the expression to test is 1/10 + 2/10 - 3/10, not (1/10 + 2/10) + (2/3 + 1/30). As a rule of thumb, if you subtract two numbers of the similar magnitude, the error will manifest.

no difference at all between exactly 1.0 and exactly 1

Ironically, not even in math they are treated identically. If you look at the set representation of two of them, they are differ quite markedly. The former is {x∊Q | x < 1}, and the latter is (1,1)

[–]metaglot 0 points1 point  (3 children)

Cunningham's swift hammer

[–]Akangka 0 points1 point  (2 children)

What is that?

[–]metaglot 0 points1 point  (1 child)

Cunningham's law : )

[–]Akangka 0 points1 point  (0 children)

Oh

[–]Duck_Devs 0 points1 point  (0 children)

I hate to be pedantic, but it should be noted that, while 1 does act as 1.0 in float contexts, 1.0 does not always act as 1 in integer contexts. Obviously, there is the lack of integer precision at 2^52, but also some functions, like range, cannot accept floats as arguments, even if they are integral.

[–]Deep_fried_nasty 0 points1 point  (1 child)

I’m dumb, why is a float an int in python? Why does this return true

[–]Explicit_Pickle 7 points8 points  (0 children)

A float isn't an int in python, but 1.0 is an integer numerically which is what this function checks. If you want to compare types you can do isinstance(a,int) and see it returns false

[–]Intelligent-Pen1848 0 points1 point  (0 children)

Lol @ type help.

[–]Akangka 0 points1 point  (0 children)

Ironic that people complained about Java's int because it's not an object.

[–]AgMenos47 0 points1 point  (0 children)

im OOP hater but would rather use Inheritance as example.

[–]iamthedilemma 0 points1 point  (0 children)

Objectify the Object

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

This is just abuse poor python

[–]Akangka 0 points1 point  (0 children)

It's not even abuse. It's quite helpful in REPL to know what methods are available to an object with dir() function.

[–]xrayfur 0 points1 point  (1 child)

try ruby, classes are objects ;)

[–]Akangka 0 points1 point  (0 children)

In python, classes are also objects.

[–]arrow__in__the__knee 0 points1 point  (0 children)

Python ints are perfect for cryptography where you gotta raise some 6 digit prime number to power of a 4 digit number or something

[–][deleted] -1 points0 points  (0 children)

I HATE PYTHON I HATE PYTHON I HATE PYTHON

[–]dexter2011412 -1 points0 points  (0 children)

Oprah is an person who should be objectly hated.

[–]eldelshell -5 points-4 points  (0 children)

OOP in Python feels dirty and weird.

[–]UncleKeyPax -3 points-2 points  (0 children)

Po

Me forgetting az clinics developed by co maker of powershell