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
#2
In most cases, people are writing classes when a function will do the same. Perhaps this is because Java was the main language to learn programming at schools. A do not use classes I barely understand them. However, I'd stick with __init__()
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
when we use constructor,it will greatly increase the line of code that we need to write,so when a copy is not needed,shouldnt it be better to not use constructor?
Reply
#4
I will tell you right away when I start to write classes intensively  Smile
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
It is not required to write an __init__ routine, it is usually included when you have data to share
between methods, or need to override an inherited value.
Reply
#6
Do you want to remember the message after printing, or is it one-time only? Then you should bind it to a name, preferrably during init, as it will be done every time and does not clutter your other code.

Quoting the python classes documentation:

Quote:The instantiation operation (“calling” a class object) creates an empty object. Many classes like to create objects with instances customized to a specific initial state. Therefore a class may define a special method named __init__(), like this:

def __init__(self):
    self.data = []
When a class defines an __init__() method, class instantiation automatically invokes __init__() for the newly-created class instance. So in this example, a new, initialized instance can be obtained by:

x = MyClass()
Of course, the __init__() method may have arguments for greater flexibility. In that case, arguments given to the class instantiation operator are passed on to __init__(). For example,

>>> class Complex:
...     def __init__(self, realpart, imagpart):
...         self.r = realpart
...         self.i = imagpart
...
>>> x = Complex(3.0, -4.5)
>>> x.r, x.i
(3.0, -4.5)
Reply
#7
Thanks everyone.
Reply
#8
This i a bad and just a confusing way.
class test():
   def say(message):
       print (message)
test.say('hello')
If place a function in a class always use @staticmethod
In almost all other cases methods has self as argument.
There are @staticmethod and @classmethod(cls as first argument) that are different.
Eg:
class Test():
    def say(self, message):
        print('i am a method in class Test: <{}>'.format(message))

    @staticmethod
    def say_func():
        print('I an i function placed in class Test')
Use:
>>> Test.say_func()
I an i function placed in class Test
>>> obj = Test()
>>> obj.say('hello')
i am a method in class Test: <hello>
>>> # can also call function from object
>>> obj.say_func()
I an i function placed in class Test
Reply
#9
Python's Instance, Class, and Static Methods Demystified

you may find this one interesting
Reply
#10
If the class is purely functional (none of it's methods dictate what any of the other methods can do), then sure, go right ahead and skip it. In that case, you're using the class almost like a namespace. You may as well just put the functions in their own file, and import the file with the name of whatever class you were going to name it.

Otherwise, you'll want to use an __init__ to set initial values for any variables you use (even if those values are all just None). This isn't php, you're not going to use isset() everywhere to check whether a variable exists.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Initiating an attribute in a class __init__: question billykid999 8 1,570 May-02-2023, 09:09 PM
Last Post: billykid999
Question __init__ of Child Class zero_fX0 4 1,915 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,465 Dec-30-2021, 04:01 PM
Last Post: mtldvl
  Not including a constructor __init__ in the class definition... bytecrunch 3 12,397 Sep-02-2021, 04:40 AM
Last Post: deanhystad
  " Run constructors" instead of the correct line MaartenRo 2 1,909 Aug-28-2020, 07:19 AM
Last Post: MaartenRo
  Why is there an __init__ statement within the __init__ definition iFunKtion 7 6,162 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