Python Forum

Full Version: Do all class need constructors?(__init__)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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?
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__()
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?
I will tell you right away when I start to write classes intensively  Smile
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.
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)
Thanks everyone.
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
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.
Pages: 1 2