Python Forum
Do all class need constructors?(__init__) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Do all class need constructors?(__init__) (/thread-2723.html)

Pages: 1 2


Do all class need constructors?(__init__) - hsunteik - Apr-05-2017

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?


RE: Do all class need constructors?(__init__) - wavic - Apr-05-2017

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__()


RE: Do all class need constructors?(__init__) - hsunteik - Apr-05-2017

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?


RE: Do all class need constructors?(__init__) - wavic - Apr-05-2017

I will tell you right away when I start to write classes intensively  Smile


RE: Do all class need constructors?(__init__) - Larz60+ - Apr-05-2017

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.


RE: Do all class need constructors?(__init__) - Kebap - Apr-05-2017

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)



RE: Do all class need constructors?(__init__) - hsunteik - Apr-05-2017

Thanks everyone.


RE: Do all class need constructors?(__init__) - snippsat - Apr-05-2017

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



RE: Do all class need constructors?(__init__) - buran - Apr-05-2017

Python's Instance, Class, and Static Methods Demystified

you may find this one interesting


RE: Do all class need constructors?(__init__) - nilamo - Apr-05-2017

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.