0

Here is an example:

# class.py
class MyString:
    def __init__(self, str):
        self.str = str
    def __div__(self, sep):
        return self.str.split(sep)

>>> m = MyString('abcdabcdabcd')
>>> print m / 'b'
['a', 'cda', 'cda', 'cd']

The __init__ method takes two parameters: the first one, self, is the instance object itself and str, the second, is a parameter passed in by the call. Is using __init__ the only way to get values into the instance of my class?

Or, if I don't declare the method __init__, will expressions like m = MyString('abcdabcdabcd') cease to work?

Aaron
  • 2,344
  • 3
  • 26
  • 32
nextdoordoc
  • 1,727
  • 6
  • 20
  • 30

4 Answers4

0

The initializer (Class.__init__()) is always used to capture the arguments passed to the constructor (Class()). Without it, the constructor won't work as desired.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0

@Ignacio got the init part. As for the question about it being the only way: no, you can get values into the instance of your class with any other function. In your example, you could have

def set_string(self, new_string):
    self.str = new_string()

or you could assign directly a value to your instance:

>>> m = MyString('abcdabcdabcd')
>>> m.str = "cbacbacba"
Tiago
  • 212
  • 4
  • 16
0

By default , when an object is created without parameter as follows

a = Class()

then the __init__ method without parameter is called .

If you are not using __init__ method , then the instances should be created without parameter as follows

class MyString:
    def __div__(self, sep):
        return self.str.split(sep)
m = MyString()

But if you want to add a parameter to your instance , you are bound to use init with parameter as in your program .

You can add class variable as follows

class MyString:
    str = ''
    def __init__(self,str):
        MyString.str = str
    def __div__(self, sep):
        return self.str.split(sep)
m = MyString('aSAA')
print(MyString.str)
#print(m.str)
['a', 'cda', 'cda', 'cd']

BUT a class variable is static and it is property of the class itself not particular to the object .

Subbu
  • 2,063
  • 4
  • 29
  • 42
0

Short answer: the recommended practice is to use the __init__ method as a "constructor" for new instances. If you do not provide this method, then you can only create instances (the normal way) of the class by calling the class name without arguments.

Longer answer:

The most straightforward alternative to using __init__ is to set attributes after the instance is created:

class Foo(object): pass
x = Foo()
x.bar = 3

A staticmethod in the class could be used to do something like this and return the instance, for example if you wanted to implement the singleton pattern. A trivial example:

class Foo(object):
  @staticmethod
  def newinstance():  # no self here
    x = Foo()
    x.bar = 3
    return x

y = Foo.newinstance()
print y.bar  # prints 3

A more esoteric way to initialize an object without actually providing an __init__ method is to use a metaclass to create instances differently than normal. But metaclasses are seldom necessary in real-world applications.

Community
  • 1
  • 1
wberry
  • 18,519
  • 8
  • 53
  • 85