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
Star NameError – function doesn't recognize imported modules Sir 4 143 Yesterday, 06:36 AM
Last Post: Sir
  Function will not load from a module Pedroski55 1 237 Jul-29-2020, 12:27 AM
Last Post: deanhystad
  from module import function krishanu21 4 462 Apr-22-2020, 10:58 AM
Last Post: buran
  Running function from parent module which has a loop in it. ta2909i 1 518 Nov-18-2019, 07:04 PM
Last Post: Gribouillis
  how to do setattr() from an imported module nutron 3 753 Sep-20-2019, 08:16 PM
Last Post: nutron
  importin function vs. import module hikerguy62 2 618 Jul-28-2019, 03:13 PM
Last Post: ndc85430
  argparse and imported modules spatialdawn 2 2,025 Dec-01-2018, 12:35 PM
Last Post: spatialdawn
  global namespace of an imported function (2 Qs) Skaperen 4 1,031 Oct-09-2018, 12:30 AM
Last Post: Skaperen
  passing a value to imported code Skaperen 0 666 Sep-28-2018, 03:59 AM
Last Post: Skaperen
  [SYS + OS] Script not Imported, Error Found ! JamieVanCadsand 2 1,642 Sep-22-2017, 08:03 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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