Python Forum
a function common to methods of a class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
a function common to methods of a class
#1
when coding a class with multiple methods, how to code a function to be common to all these methods?
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
def common_func(text):
    print(text)


class AClass:
    def multiple_method1(salf):
        common_func("multiple_method1")

    def multiple_method2(salf):
        common_func("multiple_method2")
Skaperen likes this post
Reply
#3
ah, outside the class, in the module. i was putting them inside the class (was the only class, anyway). Thanks!
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
Could make it a static method of the class as well if it would be better to keep it in the class.
Reply
#5
(Oct-03-2021, 06:31 PM)Skaperen Wrote: ah, outside the class

(Oct-03-2021, 04:56 PM)Skaperen Wrote: when coding a class with multiple methods, how to code a function to be common to all these methods?
Yes, that is what you asked for a function, not a method.
Reply
#6
(Oct-03-2021, 07:48 PM)Yoriz Wrote: Yes, that is what you asked for a function, not a method.
yes, that was for something most of the methods needed to call to avoid redundant code i was changing for various tests.

and now i am thinking back on this. it has been my understanding that a def somewhere creates a function and that a def inside a class make that function be referenced in that class' methods table. does it work some other way?

it has also been my understanding the functions def'd somewhere will see the scope they are def'd in as their global space. but then classes have been a mystery because of two spaces.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#7
(Oct-03-2021, 11:13 PM)Skaperen Wrote: it has been my understanding that a def somewhere creates a function and that a def inside a class make that function be referenced in that class' methods table. does it work some other way?
It dos not work the way you explain here.
To explain a little that may clear it up.
If want a function inside class then need to use @staticmethod,just a def inside a class make no sense and don't work.
What's make def a method is the use of self.
The reason to use staticmethod is if you have something that could be written as a standalone function (not part of any class),
but you want to keep it within the class because it's somehow semantically related to the class.
import math

class AClass:
    def foo(self):
        '''A method because of self'''
        return 42

    @staticmethod
    def circle_area(r):
        '''
        A normal function knows nothing about the class or instance
        It's still bound to the class and can be called
        '''
        return r ** 2 * math.pi

    def bar():
        '''Make no sense and should not be here'''
        return 99
Usage:
>>> # Do not require a class instance creation
>>> AClass.circle_area(5)
78.53981633974483
>>> 
>>> obj = AClass()
>>> obj.foo()
42
>>> obj.circle_area(5)
78.53981633974483
>>> obj.bar()
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
TypeError: bar() takes 0 positional arguments but 1 was given
Skaperen likes this post
Reply
#8
tuzen takk

i knew my knowledge of classes was off, but i did not know exactly where.
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
  The function of double underscore back and front in a class function name? Pedroski55 9 561 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  Class test : good way to split methods into several files paul18fr 4 403 Jan-30-2024, 11:46 AM
Last Post: Pedroski55
  Structuring a large class: privite vs public methods 6hearts 3 1,015 May-05-2023, 10:06 AM
Last Post: Gribouillis
  TimeOut a function in a class ? Armandito 1 1,586 Apr-25-2022, 04:51 PM
Last Post: Gribouillis
  Calling a class from a function jc4d 5 1,754 Dec-17-2021, 09:04 PM
Last Post: ndc85430
  access share attributed among several class methods drSlump 0 1,038 Nov-18-2021, 03:02 PM
Last Post: drSlump
  Tuple generator, and function/class syntax quazirfan 3 3,760 Aug-10-2021, 09:32 AM
Last Post: buran
  Listing All Methods Of Associated With A Class JoeDainton123 3 2,296 May-10-2021, 01:46 AM
Last Post: deanhystad
  too many methods in class - redesign idea? Phaze90 3 2,452 Mar-05-2021, 09:01 PM
Last Post: deanhystad
  Special Methods in Class Nikhil 3 2,221 Mar-04-2021, 06:25 PM
Last Post: Nikhil

Forum Jump:

User Panel Messages

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