Bottom Page

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?
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#2
Normally can just use self as your link to the class.

    def class_method(self, arg1, arg2):
        self.other_method(arg...)
Quote
#3
what if the method to reference from is __getattribute__()?
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#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.
Quote
#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).
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#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
Quote
#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).
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to emit a signal from another Class Method? SKDN 1 271 Jun-30-2020, 05:44 PM
Last Post: Yoriz
  calling on a method from one class into another class which is not a child NABA 5 219 Apr-29-2020, 07:49 PM
Last Post: deanhystad
  Theory behind referencing a dictionary rather than copying it to a list sShadowSerpent 2 212 Mar-24-2020, 07:18 PM
Last Post: sShadowSerpent
  threading for method outside class anna 0 196 Nov-17-2019, 07:05 AM
Last Post: anna
  how can I changing a base class method voidptr 2 255 Nov-10-2019, 10:53 PM
Last Post: voidptr
  How to Call a method of class having no argument dataplumber 7 818 Oct-31-2019, 01:52 PM
Last Post: dataplumber
  Call method from another method within a class anteboy65 3 504 Sep-11-2019, 08:40 PM
Last Post: Larz60+
  "not defined" error in function referencing a class Exsul 2 830 Mar-27-2019, 11:59 PM
Last Post: Exsul
  problem with class method AmirAB 3 689 Feb-13-2019, 01:51 AM
Last Post: AmirAB
  why my method doesn't find my List in the same class? Scorpio 2 558 Jan-31-2019, 05:21 PM
Last Post: Scorpio

Forum Jump:


Users browsing this thread: 1 Guest(s)