Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
anonymous method in a class
#1
i have a class named foo with a method named bar. i want to create another method named woot that can be referenced by bar but not as an attribute of foo. do i name it _woot?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Yes, a leading underscore is a common way to indicate that a method doesn't belong to the classes public interface.
Reply
#3
i know that import won't import those for modules. does that work the same way for methods of classes?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
No, they are ordinary methods, only client developer knows that he shouldn't be using this method.
Reply
#5
basically i was asking if there was a way to hide the methods like i hide functions (and variables) in a module. i'll try defining functions inside a method. this is a class with only one method (beyond init) so putting them there won't be an issue.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
Again the question is why do you want to do that? Why do you need to hide methods more than with an underscore to tell the world that this is not a public method?
Reply
#7
for things (i don't create) that gather up object attribute names and do stuff i don't want done. but i can live with this. i just wanted to know if there was a way.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
Skaperen Wrote:i just wanted to know if there was a way.

Methods are just functions after all. A simple way to hide private methods is to define them completely outside the class.
>>> class A:
...     def spam(self):
...         x = hidden(self)
...         return f'{self}: {x}'
... 
>>> def hidden(self):
...     return id(self)
... 
>>> a = A()
>>> a.spam()
'<__main__.A object at 0x7f697e056100>: 140091062575360'
Reply
#9
it will be visible in the module, so i need to name it _hidden. yeah, i can put it outside the class. then every method, when i have one than one, can use it.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  class definition and problem with a method HerrAyas 2 225 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  super() and order of running method in class inheritance akbarza 7 721 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  Using one child class method in another child class garynewport 5 1,566 Jan-11-2023, 06:07 PM
Last Post: garynewport
  [Solved] Novice question to OOP: can a method of class A access attributes of class B BigMan 1 1,306 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  class, attribute and method Frankduc 9 2,457 Feb-27-2022, 09:07 PM
Last Post: deanhystad
  Subclass initialized property used in parent class method. Is it bad coding practice? saavedra29 5 1,756 Feb-07-2022, 07:29 PM
Last Post: saavedra29
  Class Method to Calculate Age Doesn't Work gdbengo 1 1,699 Oct-30-2021, 11:20 PM
Last Post: Yoriz
  How to apply a class method to an entire dataframe column tirtha9 1 5,127 Jan-03-2021, 04:44 AM
Last Post: klllmmm
  NameError when calling a class method mfreudenberg 2 2,298 Sep-25-2020, 07:40 AM
Last Post: mfreudenberg
  Using one method's result for another method in the same Class? btownboy 3 2,339 Sep-13-2020, 06:37 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020