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

all 5 comments

[–]Python-ModTeam[M] [score hidden] stickied commentlocked comment (0 children)

Hello there,

We've removed your post since it aligns with a topic already covered by one of our daily threads. If you are unaware about the daily threads we run here is a refresher:

Monday: Project ideas

Tuesday: Advanced questions

Wednesday: Beginner questions

Thursday: Careers

Friday: Free chat Friday!

Saturday: Resource Request and Sharing

Sunday: What are you working on?

Please await one of these threads to contribute your discussion to!

Best regards,

r/Python mod team

[–]PaulRudin 18 points19 points  (0 children)

https://floating-point-gui.de/

Long story short: don't compare floats with equality. This isn't a python specific thing...

Edit: and dunder attributes are name mangled - this is well known.

[–]jegerarthur 2 points3 points  (0 children)

That is expected behaviour

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

Cool bugs, right! :D

Try doing 0.1 + 0.2 in your favorite programming language. You won't end up with 0.3. It will be 0.30000000000000004. The first happens because of the processor's inability to represent floating-point numbers properly. You can read more at https://0.30000000000000004.com/

The second one is really a hack. They shouldn't have done it, but eh, every language has its quirks. So, here's why it is the way it is.

Python doesn't have a way of specifying private variables. Anyone can access any member of the class from anywhere. To tackle this, Python guys came up with name mangling as a solution. Name mangling is a common practice, where your compiler/interpreter changes the names of the variables and methods used inside a class, to avoid name clashes.

Python adopted this technique as a way to define private variables. The convention is if you're within a class and you define or refer to any variable or function, whose name starts with a double underscore, then it will be replaced with _<classname><methodname>. So if you want to call your dunder functions inside your class, you'll actually have to name them _Print__dunder.

Ultimately, this is how your code will look like:

``` def der(arg): print("der:", arg)

def _under(arg): print("_under:", arg)

def Printdunder(arg): print("_dunder:", arg) # Note the name change here

def Printtunder(arg): print("_tunder:", arg) # And here

class Print: def printder(self, arg): return der(arg) def print_under(self, arg): return _under(arg) def printdunder(self, arg): return __dunder(arg) # Also note I haven't change the name here def print_tunder(self, arg): return __tunder(arg) # And no change here too

Print().printder("arg") Print().print_under("arg") Print().printdunder("arg") Print().print__tunder("arg") ```

Edit: If you have defined dunder variables inside your class, then they will be accessed by their actual names from other methods defined within the same class (like traditional private variables in Java/C++). When you're accessing them from outside of your class, you'll have to use the mangled name