Python Forum
function NOT imported from a module
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
function NOT imported from a module
#11
(Aug-31-2018, 06:17 AM)Skaperen Wrote: only one instance is needed when there is only one display to print to. so i really don't need a class; i can simply do this as a function.
In my view of the world, there is very little benefit in using a function instead of a class. You could provide both: a function for default display and a class to make things extendable. The function would wrap a default instance. One sees this pattern now and then in some modules, for example

class NumberOutputter:
    """Class version for extendability"""
    def __init__(self, file, maxlen):
        self.file = file
        self.maxlen = maxlen
    def output_numbers(self, *args):
         ...

import sys
default_outputter = NumberOutputter(sys.stdout, 72)

def output_numbers(*args):
    """function version for simple use"""
    return default_outputter.output_numbers(*args)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  can not import anaconda pandas module. PySpark pandas module is imported!! aupres 0 680 Aug-06-2023, 01:09 AM
Last Post: aupres
  Can a module tell where it is being imported from? stevendaprano 3 1,140 Apr-12-2022, 12:46 AM
Last Post: stevendaprano
  module detecting if imported vs not Skaperen 1 1,637 Nov-19-2021, 07:43 AM
Last Post: Yoriz
  Error when refering to class defined in 'main' in an imported module HeRo 2 2,331 Apr-13-2021, 07:22 PM
Last Post: HeRo
  [newbie] Why is a module imported twice? Winfried 3 4,037 Apr-02-2021, 04:48 AM
Last Post: deanhystad
Star NameError – function doesn't recognize imported modules Sir 4 3,417 Dec-01-2020, 06:36 AM
Last Post: Sir
  Function will not load from a module Pedroski55 1 1,772 Jul-29-2020, 12:27 AM
Last Post: deanhystad
  from module import function krishanu21 4 2,605 Apr-22-2020, 10:58 AM
Last Post: buran
  Running function from parent module which has a loop in it. ta2909i 1 2,653 Nov-18-2019, 07:04 PM
Last Post: Gribouillis
  how to do setattr() from an imported module nutron 3 3,292 Sep-20-2019, 08:16 PM
Last Post: nutron

Forum Jump:

User Panel Messages

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