Python Forum
referencing another method in a class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
referencing another method in a class
#1
how can i reference a method of a class from a different method in the same class?
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
Normally can just use self as your link to the class.

    def class_method(self, arg1, arg2):
        self.other_method(arg...)
Reply
#3
what if the method to reference from is __getattribute__()?
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
You could do that, but it would be very tricky (because the actual lookup would recursively call __getattribute__).

Is there a reason you need __getattribute__ instead of __getattr__? Doing it in the second is trivial. Doing it in the first is tricky.
Reply
#5
because i need to substitute all attribute gets with the like named attribute of the actual open file object. that means __getattr__ won't be called because __getattribute__ found the attribute. attribute "close" is the special case where i need to substitute the method in my class (so it can do os.rename() after the file is closed).

yes, i can see it is tricky. and i also have the issue of needing to store a few variables in this object instance, (1 and 2) the file names to rename,(3) the open file object that needs to be referenced and closed. that makes a lot of attributes to juggle (most reference the open file and a few not).
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
As long as you can tell the difference between when you want the method and when you don't, referencing it should be the same as always.

class MyClass:
    def __getattr__(self, attr):

        if attr == "return_method":
            # only if we're called this way do we try to return the method.
            return self.say_hi
        else:
            # __getattr__ will recursively reach here for self.say_hi and object will return it
            return object.__getattribute__(self, attr)

    def say_hi(self):
        return "Hi!"



x = MyClass()
method = x.return_method  # reference to the class method
print(method())           # call the class method
Reply
#7
this is all in my effort to create topen() a function that emulates open() as close as it can while opening files being created using a temporary name and doing a rename of the temporary name to the original name when the file is closed. a future function named ztopen() will also perform compression or decompression using the compress library open() emulations functions, based on the name of the file.

one thing i did, with success, was create a way to access the self name space without using attributes. the trick i used was to base the class from the dict type. then i can do dictionary style references to self. this avoided much complication in __getattribute__() [though most of these could probably be done less complex in __getattr__()].

topen() is now working for the basic test i am doing. i have other tests to do, including having many files concurrently open (to be sure things never get mixed up).
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
  super() and order of running method in class inheritance akbarza 7 594 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  Using one child class method in another child class garynewport 5 1,486 Jan-11-2023, 06:07 PM
Last Post: garynewport
  name 'lblstatus' is not defined when referencing a label KatManDEW 4 1,470 Apr-21-2022, 12:33 PM
Last Post: KatManDEW
  [Solved] Novice question to OOP: can a method of class A access attributes of class B BigMan 1 1,267 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  class, attribute and method Frankduc 9 2,381 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,681 Feb-07-2022, 07:29 PM
Last Post: saavedra29
  Dictionary Referencing nickdavis2017 1 1,566 Nov-20-2021, 06:24 PM
Last Post: deanhystad
  Referencing string names in df to outside variables illmattic 1 1,328 Nov-16-2021, 12:47 PM
Last Post: jefsummers
  Class Method to Calculate Age Doesn't Work gdbengo 1 1,659 Oct-30-2021, 11:20 PM
Last Post: Yoriz
  anonymous method in a class Skaperen 8 3,479 May-23-2021, 11:17 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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