Python Forum
Do all class need constructors?(__init__)
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Do all class need constructors?(__init__)
#1
I know it is all up to the programmer,but do most programmer do 
like this:

class test():
    def say(message):
        print (message)
test.say('hello')
or,like this:
class test():
    def __init__(self,message):
        self.message=message
    def say(self):
        print(self.message)

newtest=test('hello')
newtest.say()
i think using constructor is better,because i can instanciate an object to create multiple copy of the object with just one class.
what do you all think?
which is better?
Reply


Messages In This Thread
Do all class need constructors?(__init__) - by hsunteik - Apr-05-2017, 09:57 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Initiating an attribute in a class __init__: question billykid999 8 1,674 May-02-2023, 09:09 PM
Last Post: billykid999
Question __init__ of Child Class zero_fX0 4 2,134 Mar-22-2023, 05:23 PM
Last Post: deanhystad
  Python class doesn't invoke setter during __init__, not sure if's not supposed to? mtldvl 2 3,574 Dec-30-2021, 04:01 PM
Last Post: mtldvl
  Not including a constructor __init__ in the class definition... bytecrunch 3 12,872 Sep-02-2021, 04:40 AM
Last Post: deanhystad
  " Run constructors" instead of the correct line MaartenRo 2 1,974 Aug-28-2020, 07:19 AM
Last Post: MaartenRo
  Why is there an __init__ statement within the __init__ definition iFunKtion 7 6,262 Feb-06-2017, 07:31 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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