+ 1

How to have a helper function in a class ,without getting a warning of (self expected as the first parameter )

I want define a helper function thats inside a class that’s private so that the user does not have to deal with it or access The function does some algebraic multiplication it gives a warning since it expects the first element to be a class object anything to do other than ignore the warning

13th Jan 2025, 7:47 PM
Dareen Moughrabi
Dareen Moughrabi - avatar
4 RĂ©ponses
+ 3
specify what programming language. since you're saying it expects self, I'm guessing it's Python. maybe you need a @staticmethod decorator. class Foo: @staticmethod def __bar(): print('private static method') def baz(self): print('calling __bar from baz') # using __bar() inside Foo class Foo.__bar() f1 = Foo() f1.baz() #Foo.__bar() #nope, it's private #f1.__bar() #nope, it's private # but it's Python, so you can always hack it... #Foo._Foo__bar() # or #f1._Foo__bar()
14th Jan 2025, 1:25 AM
Bob_Li
Bob_Li - avatar
+ 3
Dareen Moughrabi Python doesn't have any real access specifiers built in, so if your source code is accessible, it is easy to hack. You could try compiling it to an exe. But yes, dunder methods are only fake private methods that mangles the variable name . But it is often good enough when developing your code. It's not really an issue unless you're deliberately hacking your own code and messing everything up in the process 😅
14th Jan 2025, 6:51 AM
Bob_Li
Bob_Li - avatar
+ 1
Bob_Li Yeah it is python i will try in a bit Yeah regarding private i read that we can always access a private function if we know the name of the class Is there any reason for such feature , It feels like it is just a way to tell other programs do not change but other than that it makes making a game or an app using python easy to hack by users So feels more damaging than not
14th Jan 2025, 6:42 AM
Dareen Moughrabi
Dareen Moughrabi - avatar
+ 1
Bob_Li thank you really useful information 🙏
14th Jan 2025, 7:03 AM
Dareen Moughrabi
Dareen Moughrabi - avatar