Python Forum
Building a method name in a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Building a method name in a function
#6
If you think of a Python class as a dictionary you have a pretty accurate picture of how it works. When I do a "def method_name" inside the class it adds "method_name" to a dictionary in the class with a value that is the class.method_name function. Creating a class variable does the same thing, except the value in the dictionary is the object referenced by the class variable name.

Since classes are essentially dictionaries with some syntactic sugar, you can add methods from anywhere and at any time. You can also add methods to instances of classes so that two instances do different things when the same method name is used. This is usually a bad idea, but there are times when it can be useful.

I don't know what you are trying to do, but here is something I did which may be similar. I have a class named Model. A model is a collection of models/parts. I have no idea what parts a model may contain or what those parts can do, but I commonly ask all a model's parts to do the same thing. For this I use "forall".
    def forall(self, func_name, *args, **kwargs):
        """Have all my parts execute the named function"""
        for part in self._parts:
            if (func := getattr(part, func_name, None)) is not None:
                func(part, *args, **kwargs)
        return self
Reply


Messages In This Thread
Building a method name in a function - by ffgth - Oct-18-2020, 10:41 PM
RE: Building a method name in a function - by ffgth - Oct-18-2020, 10:55 PM
RE: Building a method name in a function - by buran - Oct-19-2020, 03:27 AM
RE: Building a method name in a function - by ffgth - Oct-19-2020, 11:35 AM
RE: Building a method name in a function - by deanhystad - Oct-19-2020, 04:05 AM
RE: Building a method name in a function - by buran - Oct-19-2020, 11:57 AM
RE: Building a method name in a function - by ffgth - Oct-19-2020, 01:01 PM
RE: Building a method name in a function - by buran - Oct-19-2020, 01:21 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Difference between method and attribute holding a function ulrich 2 935 Jun-30-2024, 08:35 AM
Last Post: snippsat
  Building a DoublyLinkedList in Python - - append method Drone4four 2 1,258 Jan-08-2024, 01:27 PM
Last Post: Drone4four
  i want to use type= as a function/method keyword argument Skaperen 9 3,604 Nov-06-2022, 04:28 AM
Last Post: Skaperen
  function/method help myv5285 3 3,780 May-17-2020, 04:19 AM
Last Post: buran
  Accessing method as function object ClassicalSoul 2 2,722 Feb-14-2020, 09:31 PM
Last Post: wavic
  got SyntaxError while building simple function zarize 2 2,853 Feb-14-2020, 10:51 AM
Last Post: zarize
  function vs method prateekshaw 2 2,802 Nov-14-2019, 07:00 PM
Last Post: DeaD_EyE
  I'm trying to figure out whether this is a method or function call 357mag 2 3,205 Jul-04-2019, 01:43 AM
Last Post: ichabod801
  function method problems drchar 2 3,775 Dec-11-2018, 02:38 PM
Last Post: buran
  Understanding Method/Function robdhunter 2 3,299 Mar-10-2018, 11:57 PM
Last Post: robdhunter

Forum Jump:

User Panel Messages

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