Must all Python instance variables be declared in def __init__? - python

Or can they be declared otherwise?
The code below does not work:
class BinaryNode():
self.parent = None
self.left_child = None
Do they need to be declared in __init__?

They do not have to be declared in __init__, but in order to set an instance variable using self, there needs to be a reference to self, and the place you are defining the variables does not.
However,
class BinaryNode():
parent = None
left_child = None
def run(self):
self.parent = "Foo"
print self.parent
print self.left_child
The output will be
Foo
None
To answer your question in the comment, yes. You can, in my example say:
bn = BinaryNode()
bn.new_variable = "Bar"
Or, as I showed, you can set a class level variable. All new instances of the class will get a copy of the class level variables at instantiation.
Perhaps you are not aware that you can pass arguments to the constructor:
class BinaryNode(object):
def __init__(self, parent=None, left_child=None):
self.parent = parent
self.left_child = left_child
bn = BinaryNode(node_parent, node_to_the_left)

Must all Python instance variables be declared in def __init__?
Short answer: no. But it really depends on how/where to use them.
Or can they be declared otherwise?
Yes they can be declared otherwise (not an instance variable). Since you're referring to variables inside a class, yes you can declare a local variable (for example) inside of it if you don't want to use __init__ constructor function that uses the keyword self.
The code below does not work:
class BinaryNode():
self.parent = None
self.left_child = None
What do you mean by not working? if you're referring to how most developers use a class, then you are right, it does not work in the sense that you can not create a class object (it's also called class instance) and then call one of its instance variables assuming they are declared properly (ie: declared INSIDE the init method that plays the role of the constructor method.)
In other words: if you declare a variable outside the constructor method init then it's called a class variable and not an instance variable.
To initiate an instance variable, it needs to be inside the init method that will actually construct (instantiate) the class instance (class object) along with its associated instance variables (instance attributes.)
for example:
if you have parent = None instead of self.parent = None you just created a local variable inside your class.
So the word self is there to hold the place for when you call the class instance is called, it's passed-in in lieu of self. Therefor the word self is just there as a parameter to hold a place for passing whatever instance variable we want to call in its place. So the word self is just doing a favor service here.
Similarly , when creating a class object and say you want to create an instance variable, you must use the attribute word self (it does not have to be self but since most python developers use it, using it became a convention and a good practice in python) that will work as a reference (a holder) to complete the work of the constructor function __init__ which will assign the argument value passed-in the class instance (object) created to be the new value of the instance variable copy that belongs to the class instance created.
Now, let's try another scenario to better solidify the concept:
What if we use the __init__ function but not the keyword self?
if you create a class object named object_x and then you want to call one of its instance variables named instance_y from the , then you execute the program, it will throw an error saying that the class object created does not have an attribute (instance variable).
It will be just a local variable with whatever value you assigned to it when you defined it.
The error occurs because it doesn't recognize it as one of the instance variables defined inside the class when it's missing the reference word self.

Nope. I love the #property variable for just this thing:
class Data(object):
"""give me some data, and I'll give you more"""
def __init__(self, some, others):
self.some = some
self.others = others
#property
def more(self):
"""you don't instantiate this with __init__, per say..."""
return zip(self.some, self.others)
>>> mydata = Data([1, 2, 3], ['a', 'b', 'c'])
>>> mydata.more
[(1, 'a'), (2, 'b'), (3, 'c')]

Also, you can have class level variables, but I call them class constants.
class Connection(object):
"""helps you connect to the server at work"""
YOUR_IP = '10.0.9.99'
def __init__(self, username, password):
self.ip = Connection.YOUR_IP
self.un = username
self.pw = password
#...and so on

Related

AttributeError: type object 'Vehicle' has no attribute 'rect' [duplicate]

This question already has answers here:
What is the purpose of the `self` parameter? Why is it needed?
(26 answers)
Why do we use __init__ in Python classes?
(9 answers)
Closed 6 months ago.
I'm learning the Python programming language and I've came across something I don't fully understand.
In a method like:
def method(self, blah):
def __init__(?):
....
....
What does self do? What is it meant to be? Is it mandatory?
What does the __init__ method do? Why is it necessary? (etc.)
I think they might be OOP constructs, but I don't know very much.
In this code:
class A(object):
def __init__(self):
self.x = 'Hello'
def method_a(self, foo):
print self.x + ' ' + foo
... the self variable represents the instance of the object itself. Most object-oriented languages pass this as a hidden parameter to the methods defined on an object; Python does not. You have to declare it explicitly. When you create an instance of the A class and call its methods, it will be passed automatically, as in ...
a = A() # We do not pass any argument to the __init__ method
a.method_a('Sailor!') # We only pass a single argument
The __init__ method is roughly what represents a constructor in Python. When you call A() Python creates an object for you, and passes it as the first parameter to the __init__ method. Any additional parameters (e.g., A(24, 'Hello')) will also get passed as arguments--in this case causing an exception to be raised, since the constructor isn't expecting them.
Yep, you are right, these are oop constructs.
__init__ is the constructor for a class. The self parameter refers to the instance of the object (like this in C++).
class Point:
def __init__(self, x, y):
self._x = x
self._y = y
The __init__ method gets called after memory for the object is allocated:
x = Point(1,2)
It is important to use the self parameter inside an object's method if you want to persist the value with the object. If, for instance, you implement the __init__ method like this:
class Point:
def __init__(self, x, y):
_x = x
_y = y
Your x and y parameters would be stored in variables on the stack and would be discarded when the init method goes out of scope. Setting those variables as self._x and self._y sets those variables as members of the Point object (accessible for the lifetime of the object).
N.B. Some clarification of the use of the word "constructor" in this answer. Technically the responsibilities of a "constructor" are split over two methods in Python. Those methods are __new__ (responsible for allocating memory) and __init__ (as discussed here, responsible for initialising the newly created instance).
A brief illustrative example
In the hope it might help a little, here's a simple example I used to understand the difference between a variable declared inside a class, and a variable declared inside an __init__ function:
class MyClass(object):
i = 123
def __init__(self):
self.i = 345
a = MyClass()
print(a.i)
print(MyClass.i)
Output:
345
123
In short:
self as it suggests, refers to itself- the object which has called the method. That is, if you have N objects calling the method, then self.a will refer to a separate instance of the variable for each of the N objects. Imagine N copies of the variable a for each object
__init__ is what is called as a constructor in other OOP languages such as C++/Java. The basic idea is that it is a special method which is automatically called when an object of that Class is created
Class objects support two kinds of operations: attribute references and instantiation
Attribute references use the standard syntax used for all attribute references in Python: obj.name. Valid attribute names are all the names that were in the class’s namespace when the class object was created. So, if the class definition looked like this:
class MyClass:
"""A simple example class"""
i = 12345
def f(self):
return 'hello world'
then MyClass.i and MyClass.f are valid attribute references, returning an integer and a function object, respectively. Class attributes can also be assigned to, so you can change the value of MyClass.i by assignment. __doc__ is also a valid attribute, returning the docstring belonging to the class: "A simple example class".
Class instantiation uses function notation. Just pretend that the class object is a parameterless function that returns a new instance of the class. For example:
x = MyClass()
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
Taken from official documentation which helped me the most in the end.
Here is my example
class Bill():
def __init__(self,apples,figs,dates):
self.apples = apples
self.figs = figs
self.dates = dates
self.bill = apples + figs + dates
print ("Buy",self.apples,"apples", self.figs,"figs
and",self.dates,"dates.
Total fruitty bill is",self.bill," pieces of fruit :)")
When you create instance of class Bill:
purchase = Bill(5,6,7)
You get:
> Buy 5 apples 6 figs and 7 dates. Total fruitty bill is 18 pieces of
> fruit :)
__init__ does act like a constructor. You'll need to pass "self" to any class functions as the first argument if you want them to behave as non-static methods. "self" are instance variables for your class.
Try out this code. Hope it helps many C programmers like me to Learn Py.
#! /usr/bin/python2
class Person:
'''Doc - Inside Class '''
def __init__(self, name):
'''Doc - __init__ Constructor'''
self.n_name = name
def show(self, n1, n2):
'''Doc - Inside Show'''
print self.n_name
print 'Sum = ', (n1 + n2)
def __del__(self):
print 'Destructor Deleting object - ', self.n_name
p=Person('Jay')
p.show(2, 3)
print p.__doc__
print p.__init__.__doc__
print p.show.__doc__
Output:
Jay
Sum = 5
Doc - Inside Class
Doc - __init__ Constructor
Doc - Inside Show
Destructor Deleting object - Jay
Had trouble undestanding this myself. Even after reading the answers here.
To properly understand the __init__ method you need to understand self.
The self Parameter
The arguments accepted by the __init__ method are :
def __init__(self, arg1, arg2):
But we only actually pass it two arguments :
instance = OurClass('arg1', 'arg2')
Where has the extra argument come from ?
When we access attributes of an object we do it by name (or by reference). Here instance is a reference to our new object. We access the printargs method of the instance object using instance.printargs.
In order to access object attributes from within the __init__ method we need a reference to the object.
Whenever a method is called, a reference to the main object is passed as the first argument. By convention you always call this first argument to your methods self.
This means in the __init__ method we can do :
self.arg1 = arg1
self.arg2 = arg2
Here we are setting attributes on the object. You can verify this by doing the following :
instance = OurClass('arg1', 'arg2')
print instance.arg1
arg1
values like this are known as object attributes. Here the __init__ method sets the arg1 and arg2 attributes of the instance.
source: http://www.voidspace.org.uk/python/articles/OOP.shtml#the-init-method
note that self could actually be any valid python identifier. For example, we could just as easily write, from Chris B's example:
class A(object):
def __init__(foo):
foo.x = 'Hello'
def method_a(bar, foo):
print bar.x + ' ' + foo
and it would work exactly the same. It is however recommended to use self because other pythoners will recognize it more easily.
What does self do? What is it meant to be? Is it mandatory?
The first argument of every class method, including init, is always a reference to the current instance of the class. By convention, this argument is always named self. In the init method, self refers to the newly created object; in other class methods, it refers to the instance whose method was called.
Python doesn't force you on using "self". You can give it any name you want. But remember the first argument in a method definition is a reference to the object. Python adds the self argument to the list for you; you do not need to include it when you call the methods.
if you didn't provide self in init method then you will get an error
TypeError: __init___() takes no arguments (1 given)
What does the init method do? Why is it necessary? (etc.)
init is short for initialization. It is a constructor which gets called when you make an instance of the class and it is not necessary. But usually it our practice to write init method for setting default state of the object. If you are not willing to set any state of the object initially then you don't need to write this method.
__init__ is basically a function which will "initialize"/"activate" the properties of the class for a specific object, once created and matched to the corresponding class..
self represents that object which will inherit those properties.
Basically, you need to use the 'self' keyword when using a variable in multiple functions within the same class. As for init, it's used to setup default values incase no other functions from within that class are called.
The 'self' is a reference to the class instance
class foo:
def bar(self):
print "hi"
Now we can create an instance of foo and call the method on it, the self parameter is added by Python in this case:
f = foo()
f.bar()
But it can be passed in as well if the method call isn't in the context of an instance of the class, the code below does the same thing
f = foo()
foo.bar(f)
Interestingly the variable name 'self' is just a convention. The below definition will work exactly the same.. Having said that it is very strong convention which should be followed always, but it does say something about flexible nature of the language
class foo:
def bar(s):
print "hi"
Just a demo for the question.
class MyClass:
def __init__(self):
print('__init__ is the constructor for a class')
def __del__(self):
print('__del__ is the destructor for a class')
def __enter__(self):
print('__enter__ is for context manager')
return self
def __exit__(self, exc_type, exc_value, traceback):
print('__exit__ is for context manager')
def greeting(self):
print('hello python')
if __name__ == '__main__':
with MyClass() as mycls:
mycls.greeting()
$ python3 class.objects_instantiation.py
__init__ is the constructor for a class
__enter__ is for context manager
hello python
__exit__ is for context manager
__del__ is the destructor for a class
In this code:
class Cat:
def __init__(self, name):
self.name = name
def info(self):
print 'I am a cat and I am called', self.name
Here __init__ acts as a constructor for the class and when an object is instantiated, this function is called. self represents the instantiating object.
c = Cat('Kitty')
c.info()
The result of the above statements will be as follows:
I am a cat and I am called Kitty
# Source: Class and Instance Variables
# https://docs.python.org/2/tutorial/classes.html#class-and-instance-variables
class MyClass(object):
# class variable
my_CLS_var = 10
# sets "init'ial" state to objects/instances, use self argument
def __init__(self):
# self usage => instance variable (per object)
self.my_OBJ_var = 15
# also possible, class name is used => init class variable
MyClass.my_CLS_var = 20
def run_example_func():
# PRINTS 10 (class variable)
print MyClass.my_CLS_var
# executes __init__ for obj1 instance
# NOTE: __init__ changes class variable above
obj1 = MyClass()
# PRINTS 15 (instance variable)
print obj1.my_OBJ_var
# PRINTS 20 (class variable, changed value)
print MyClass.my_CLS_var
run_example_func()
Here, the guy has written pretty well and simple: https://www.jeffknupp.com/blog/2014/06/18/improve-your-python-python-classes-and-object-oriented-programming/
Read above link as a reference to this:
self? So what's with that self parameter to all of the Customer
methods? What is it? Why, it's the instance, of course! Put another
way, a method like withdraw defines the instructions for withdrawing
money from some abstract customer's account. Calling
jeff.withdraw(100.0) puts those instructions to use on the jeff
instance.
So when we say def withdraw(self, amount):, we're saying, "here's how
you withdraw money from a Customer object (which we'll call self) and
a dollar figure (which we'll call amount). self is the instance of the
Customer that withdraw is being called on. That's not me making
analogies, either. jeff.withdraw(100.0) is just shorthand for
Customer.withdraw(jeff, 100.0), which is perfectly valid (if not often
seen) code.
init self may make sense for other methods, but what about init? When we call init, we're in the process of creating an object, so how can there already be a self? Python allows us to extend
the self pattern to when objects are constructed as well, even though
it doesn't exactly fit. Just imagine that jeff = Customer('Jeff
Knupp', 1000.0) is the same as calling jeff = Customer(jeff, 'Jeff
Knupp', 1000.0); the jeff that's passed in is also made the result.
This is why when we call init, we initialize objects by saying
things like self.name = name. Remember, since self is the instance,
this is equivalent to saying jeff.name = name, which is the same as
jeff.name = 'Jeff Knupp. Similarly, self.balance = balance is the same
as jeff.balance = 1000.0. After these two lines, we consider the
Customer object "initialized" and ready for use.
Be careful what you __init__
After init has finished, the caller can rightly assume that the
object is ready to use. That is, after jeff = Customer('Jeff Knupp',
1000.0), we can start making deposit and withdraw calls on jeff; jeff is a fully-initialized object.
Python __init__ and self what do they do?
What does self do? What is it meant to be? Is it mandatory?
What does the __init__ method do? Why is it necessary? (etc.)
The example given is not correct, so let me create a correct example based on it:
class SomeObject(object):
def __init__(self, blah):
self.blah = blah
def method(self):
return self.blah
When we create an instance of the object, the __init__ is called to customize the object after it has been created. That is, when we call SomeObject with 'blah' below (which could be anything), it gets passed to the __init__ function as the argument, blah:
an_object = SomeObject('blah')
The self argument is the instance of SomeObject that will be assigned to an_object.
Later, we might want to call a method on this object:
an_object.method()
Doing the dotted lookup, that is, an_object.method, binds the instance to an instance of the function, and the method (as called above) is now a "bound" method - which means we do not need to explicitly pass the instance to the method call.
The method call gets the instance because it was bound on the dotted lookup, and when called, then executes whatever code it was programmed to perform.
The implicitly passed self argument is called self by convention. We could use any other legal Python name, but you will likely get tarred and feathered by other Python programmers if you change it to something else.
__init__ is a special method, documented in the Python datamodel documentation. It is called immediately after the instance is created (usually via __new__ - although __new__ is not required unless you are subclassing an immutable datatype).

Python/Console returning 'self' is not defined [duplicate]

This question already has answers here:
What is the purpose of the `self` parameter? Why is it needed?
(26 answers)
Why do we use __init__ in Python classes?
(9 answers)
Closed 6 months ago.
I'm learning the Python programming language and I've came across something I don't fully understand.
In a method like:
def method(self, blah):
def __init__(?):
....
....
What does self do? What is it meant to be? Is it mandatory?
What does the __init__ method do? Why is it necessary? (etc.)
I think they might be OOP constructs, but I don't know very much.
In this code:
class A(object):
def __init__(self):
self.x = 'Hello'
def method_a(self, foo):
print self.x + ' ' + foo
... the self variable represents the instance of the object itself. Most object-oriented languages pass this as a hidden parameter to the methods defined on an object; Python does not. You have to declare it explicitly. When you create an instance of the A class and call its methods, it will be passed automatically, as in ...
a = A() # We do not pass any argument to the __init__ method
a.method_a('Sailor!') # We only pass a single argument
The __init__ method is roughly what represents a constructor in Python. When you call A() Python creates an object for you, and passes it as the first parameter to the __init__ method. Any additional parameters (e.g., A(24, 'Hello')) will also get passed as arguments--in this case causing an exception to be raised, since the constructor isn't expecting them.
Yep, you are right, these are oop constructs.
__init__ is the constructor for a class. The self parameter refers to the instance of the object (like this in C++).
class Point:
def __init__(self, x, y):
self._x = x
self._y = y
The __init__ method gets called after memory for the object is allocated:
x = Point(1,2)
It is important to use the self parameter inside an object's method if you want to persist the value with the object. If, for instance, you implement the __init__ method like this:
class Point:
def __init__(self, x, y):
_x = x
_y = y
Your x and y parameters would be stored in variables on the stack and would be discarded when the init method goes out of scope. Setting those variables as self._x and self._y sets those variables as members of the Point object (accessible for the lifetime of the object).
N.B. Some clarification of the use of the word "constructor" in this answer. Technically the responsibilities of a "constructor" are split over two methods in Python. Those methods are __new__ (responsible for allocating memory) and __init__ (as discussed here, responsible for initialising the newly created instance).
A brief illustrative example
In the hope it might help a little, here's a simple example I used to understand the difference between a variable declared inside a class, and a variable declared inside an __init__ function:
class MyClass(object):
i = 123
def __init__(self):
self.i = 345
a = MyClass()
print(a.i)
print(MyClass.i)
Output:
345
123
In short:
self as it suggests, refers to itself- the object which has called the method. That is, if you have N objects calling the method, then self.a will refer to a separate instance of the variable for each of the N objects. Imagine N copies of the variable a for each object
__init__ is what is called as a constructor in other OOP languages such as C++/Java. The basic idea is that it is a special method which is automatically called when an object of that Class is created
Class objects support two kinds of operations: attribute references and instantiation
Attribute references use the standard syntax used for all attribute references in Python: obj.name. Valid attribute names are all the names that were in the class’s namespace when the class object was created. So, if the class definition looked like this:
class MyClass:
"""A simple example class"""
i = 12345
def f(self):
return 'hello world'
then MyClass.i and MyClass.f are valid attribute references, returning an integer and a function object, respectively. Class attributes can also be assigned to, so you can change the value of MyClass.i by assignment. __doc__ is also a valid attribute, returning the docstring belonging to the class: "A simple example class".
Class instantiation uses function notation. Just pretend that the class object is a parameterless function that returns a new instance of the class. For example:
x = MyClass()
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
Taken from official documentation which helped me the most in the end.
Here is my example
class Bill():
def __init__(self,apples,figs,dates):
self.apples = apples
self.figs = figs
self.dates = dates
self.bill = apples + figs + dates
print ("Buy",self.apples,"apples", self.figs,"figs
and",self.dates,"dates.
Total fruitty bill is",self.bill," pieces of fruit :)")
When you create instance of class Bill:
purchase = Bill(5,6,7)
You get:
> Buy 5 apples 6 figs and 7 dates. Total fruitty bill is 18 pieces of
> fruit :)
__init__ does act like a constructor. You'll need to pass "self" to any class functions as the first argument if you want them to behave as non-static methods. "self" are instance variables for your class.
Try out this code. Hope it helps many C programmers like me to Learn Py.
#! /usr/bin/python2
class Person:
'''Doc - Inside Class '''
def __init__(self, name):
'''Doc - __init__ Constructor'''
self.n_name = name
def show(self, n1, n2):
'''Doc - Inside Show'''
print self.n_name
print 'Sum = ', (n1 + n2)
def __del__(self):
print 'Destructor Deleting object - ', self.n_name
p=Person('Jay')
p.show(2, 3)
print p.__doc__
print p.__init__.__doc__
print p.show.__doc__
Output:
Jay
Sum = 5
Doc - Inside Class
Doc - __init__ Constructor
Doc - Inside Show
Destructor Deleting object - Jay
Had trouble undestanding this myself. Even after reading the answers here.
To properly understand the __init__ method you need to understand self.
The self Parameter
The arguments accepted by the __init__ method are :
def __init__(self, arg1, arg2):
But we only actually pass it two arguments :
instance = OurClass('arg1', 'arg2')
Where has the extra argument come from ?
When we access attributes of an object we do it by name (or by reference). Here instance is a reference to our new object. We access the printargs method of the instance object using instance.printargs.
In order to access object attributes from within the __init__ method we need a reference to the object.
Whenever a method is called, a reference to the main object is passed as the first argument. By convention you always call this first argument to your methods self.
This means in the __init__ method we can do :
self.arg1 = arg1
self.arg2 = arg2
Here we are setting attributes on the object. You can verify this by doing the following :
instance = OurClass('arg1', 'arg2')
print instance.arg1
arg1
values like this are known as object attributes. Here the __init__ method sets the arg1 and arg2 attributes of the instance.
source: http://www.voidspace.org.uk/python/articles/OOP.shtml#the-init-method
note that self could actually be any valid python identifier. For example, we could just as easily write, from Chris B's example:
class A(object):
def __init__(foo):
foo.x = 'Hello'
def method_a(bar, foo):
print bar.x + ' ' + foo
and it would work exactly the same. It is however recommended to use self because other pythoners will recognize it more easily.
What does self do? What is it meant to be? Is it mandatory?
The first argument of every class method, including init, is always a reference to the current instance of the class. By convention, this argument is always named self. In the init method, self refers to the newly created object; in other class methods, it refers to the instance whose method was called.
Python doesn't force you on using "self". You can give it any name you want. But remember the first argument in a method definition is a reference to the object. Python adds the self argument to the list for you; you do not need to include it when you call the methods.
if you didn't provide self in init method then you will get an error
TypeError: __init___() takes no arguments (1 given)
What does the init method do? Why is it necessary? (etc.)
init is short for initialization. It is a constructor which gets called when you make an instance of the class and it is not necessary. But usually it our practice to write init method for setting default state of the object. If you are not willing to set any state of the object initially then you don't need to write this method.
__init__ is basically a function which will "initialize"/"activate" the properties of the class for a specific object, once created and matched to the corresponding class..
self represents that object which will inherit those properties.
Basically, you need to use the 'self' keyword when using a variable in multiple functions within the same class. As for init, it's used to setup default values incase no other functions from within that class are called.
The 'self' is a reference to the class instance
class foo:
def bar(self):
print "hi"
Now we can create an instance of foo and call the method on it, the self parameter is added by Python in this case:
f = foo()
f.bar()
But it can be passed in as well if the method call isn't in the context of an instance of the class, the code below does the same thing
f = foo()
foo.bar(f)
Interestingly the variable name 'self' is just a convention. The below definition will work exactly the same.. Having said that it is very strong convention which should be followed always, but it does say something about flexible nature of the language
class foo:
def bar(s):
print "hi"
Just a demo for the question.
class MyClass:
def __init__(self):
print('__init__ is the constructor for a class')
def __del__(self):
print('__del__ is the destructor for a class')
def __enter__(self):
print('__enter__ is for context manager')
return self
def __exit__(self, exc_type, exc_value, traceback):
print('__exit__ is for context manager')
def greeting(self):
print('hello python')
if __name__ == '__main__':
with MyClass() as mycls:
mycls.greeting()
$ python3 class.objects_instantiation.py
__init__ is the constructor for a class
__enter__ is for context manager
hello python
__exit__ is for context manager
__del__ is the destructor for a class
In this code:
class Cat:
def __init__(self, name):
self.name = name
def info(self):
print 'I am a cat and I am called', self.name
Here __init__ acts as a constructor for the class and when an object is instantiated, this function is called. self represents the instantiating object.
c = Cat('Kitty')
c.info()
The result of the above statements will be as follows:
I am a cat and I am called Kitty
# Source: Class and Instance Variables
# https://docs.python.org/2/tutorial/classes.html#class-and-instance-variables
class MyClass(object):
# class variable
my_CLS_var = 10
# sets "init'ial" state to objects/instances, use self argument
def __init__(self):
# self usage => instance variable (per object)
self.my_OBJ_var = 15
# also possible, class name is used => init class variable
MyClass.my_CLS_var = 20
def run_example_func():
# PRINTS 10 (class variable)
print MyClass.my_CLS_var
# executes __init__ for obj1 instance
# NOTE: __init__ changes class variable above
obj1 = MyClass()
# PRINTS 15 (instance variable)
print obj1.my_OBJ_var
# PRINTS 20 (class variable, changed value)
print MyClass.my_CLS_var
run_example_func()
Here, the guy has written pretty well and simple: https://www.jeffknupp.com/blog/2014/06/18/improve-your-python-python-classes-and-object-oriented-programming/
Read above link as a reference to this:
self? So what's with that self parameter to all of the Customer
methods? What is it? Why, it's the instance, of course! Put another
way, a method like withdraw defines the instructions for withdrawing
money from some abstract customer's account. Calling
jeff.withdraw(100.0) puts those instructions to use on the jeff
instance.
So when we say def withdraw(self, amount):, we're saying, "here's how
you withdraw money from a Customer object (which we'll call self) and
a dollar figure (which we'll call amount). self is the instance of the
Customer that withdraw is being called on. That's not me making
analogies, either. jeff.withdraw(100.0) is just shorthand for
Customer.withdraw(jeff, 100.0), which is perfectly valid (if not often
seen) code.
init self may make sense for other methods, but what about init? When we call init, we're in the process of creating an object, so how can there already be a self? Python allows us to extend
the self pattern to when objects are constructed as well, even though
it doesn't exactly fit. Just imagine that jeff = Customer('Jeff
Knupp', 1000.0) is the same as calling jeff = Customer(jeff, 'Jeff
Knupp', 1000.0); the jeff that's passed in is also made the result.
This is why when we call init, we initialize objects by saying
things like self.name = name. Remember, since self is the instance,
this is equivalent to saying jeff.name = name, which is the same as
jeff.name = 'Jeff Knupp. Similarly, self.balance = balance is the same
as jeff.balance = 1000.0. After these two lines, we consider the
Customer object "initialized" and ready for use.
Be careful what you __init__
After init has finished, the caller can rightly assume that the
object is ready to use. That is, after jeff = Customer('Jeff Knupp',
1000.0), we can start making deposit and withdraw calls on jeff; jeff is a fully-initialized object.
Python __init__ and self what do they do?
What does self do? What is it meant to be? Is it mandatory?
What does the __init__ method do? Why is it necessary? (etc.)
The example given is not correct, so let me create a correct example based on it:
class SomeObject(object):
def __init__(self, blah):
self.blah = blah
def method(self):
return self.blah
When we create an instance of the object, the __init__ is called to customize the object after it has been created. That is, when we call SomeObject with 'blah' below (which could be anything), it gets passed to the __init__ function as the argument, blah:
an_object = SomeObject('blah')
The self argument is the instance of SomeObject that will be assigned to an_object.
Later, we might want to call a method on this object:
an_object.method()
Doing the dotted lookup, that is, an_object.method, binds the instance to an instance of the function, and the method (as called above) is now a "bound" method - which means we do not need to explicitly pass the instance to the method call.
The method call gets the instance because it was bound on the dotted lookup, and when called, then executes whatever code it was programmed to perform.
The implicitly passed self argument is called self by convention. We could use any other legal Python name, but you will likely get tarred and feathered by other Python programmers if you change it to something else.
__init__ is a special method, documented in the Python datamodel documentation. It is called immediately after the instance is created (usually via __new__ - although __new__ is not required unless you are subclassing an immutable datatype).

What needs to be inside of __init__.py [duplicate]

This question already has answers here:
What is the purpose of the `self` parameter? Why is it needed?
(26 answers)
Why do we use __init__ in Python classes?
(9 answers)
Closed 6 months ago.
I'm learning the Python programming language and I've came across something I don't fully understand.
In a method like:
def method(self, blah):
def __init__(?):
....
....
What does self do? What is it meant to be? Is it mandatory?
What does the __init__ method do? Why is it necessary? (etc.)
I think they might be OOP constructs, but I don't know very much.
In this code:
class A(object):
def __init__(self):
self.x = 'Hello'
def method_a(self, foo):
print self.x + ' ' + foo
... the self variable represents the instance of the object itself. Most object-oriented languages pass this as a hidden parameter to the methods defined on an object; Python does not. You have to declare it explicitly. When you create an instance of the A class and call its methods, it will be passed automatically, as in ...
a = A() # We do not pass any argument to the __init__ method
a.method_a('Sailor!') # We only pass a single argument
The __init__ method is roughly what represents a constructor in Python. When you call A() Python creates an object for you, and passes it as the first parameter to the __init__ method. Any additional parameters (e.g., A(24, 'Hello')) will also get passed as arguments--in this case causing an exception to be raised, since the constructor isn't expecting them.
Yep, you are right, these are oop constructs.
__init__ is the constructor for a class. The self parameter refers to the instance of the object (like this in C++).
class Point:
def __init__(self, x, y):
self._x = x
self._y = y
The __init__ method gets called after memory for the object is allocated:
x = Point(1,2)
It is important to use the self parameter inside an object's method if you want to persist the value with the object. If, for instance, you implement the __init__ method like this:
class Point:
def __init__(self, x, y):
_x = x
_y = y
Your x and y parameters would be stored in variables on the stack and would be discarded when the init method goes out of scope. Setting those variables as self._x and self._y sets those variables as members of the Point object (accessible for the lifetime of the object).
N.B. Some clarification of the use of the word "constructor" in this answer. Technically the responsibilities of a "constructor" are split over two methods in Python. Those methods are __new__ (responsible for allocating memory) and __init__ (as discussed here, responsible for initialising the newly created instance).
A brief illustrative example
In the hope it might help a little, here's a simple example I used to understand the difference between a variable declared inside a class, and a variable declared inside an __init__ function:
class MyClass(object):
i = 123
def __init__(self):
self.i = 345
a = MyClass()
print(a.i)
print(MyClass.i)
Output:
345
123
In short:
self as it suggests, refers to itself- the object which has called the method. That is, if you have N objects calling the method, then self.a will refer to a separate instance of the variable for each of the N objects. Imagine N copies of the variable a for each object
__init__ is what is called as a constructor in other OOP languages such as C++/Java. The basic idea is that it is a special method which is automatically called when an object of that Class is created
Class objects support two kinds of operations: attribute references and instantiation
Attribute references use the standard syntax used for all attribute references in Python: obj.name. Valid attribute names are all the names that were in the class’s namespace when the class object was created. So, if the class definition looked like this:
class MyClass:
"""A simple example class"""
i = 12345
def f(self):
return 'hello world'
then MyClass.i and MyClass.f are valid attribute references, returning an integer and a function object, respectively. Class attributes can also be assigned to, so you can change the value of MyClass.i by assignment. __doc__ is also a valid attribute, returning the docstring belonging to the class: "A simple example class".
Class instantiation uses function notation. Just pretend that the class object is a parameterless function that returns a new instance of the class. For example:
x = MyClass()
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
Taken from official documentation which helped me the most in the end.
Here is my example
class Bill():
def __init__(self,apples,figs,dates):
self.apples = apples
self.figs = figs
self.dates = dates
self.bill = apples + figs + dates
print ("Buy",self.apples,"apples", self.figs,"figs
and",self.dates,"dates.
Total fruitty bill is",self.bill," pieces of fruit :)")
When you create instance of class Bill:
purchase = Bill(5,6,7)
You get:
> Buy 5 apples 6 figs and 7 dates. Total fruitty bill is 18 pieces of
> fruit :)
__init__ does act like a constructor. You'll need to pass "self" to any class functions as the first argument if you want them to behave as non-static methods. "self" are instance variables for your class.
Try out this code. Hope it helps many C programmers like me to Learn Py.
#! /usr/bin/python2
class Person:
'''Doc - Inside Class '''
def __init__(self, name):
'''Doc - __init__ Constructor'''
self.n_name = name
def show(self, n1, n2):
'''Doc - Inside Show'''
print self.n_name
print 'Sum = ', (n1 + n2)
def __del__(self):
print 'Destructor Deleting object - ', self.n_name
p=Person('Jay')
p.show(2, 3)
print p.__doc__
print p.__init__.__doc__
print p.show.__doc__
Output:
Jay
Sum = 5
Doc - Inside Class
Doc - __init__ Constructor
Doc - Inside Show
Destructor Deleting object - Jay
Had trouble undestanding this myself. Even after reading the answers here.
To properly understand the __init__ method you need to understand self.
The self Parameter
The arguments accepted by the __init__ method are :
def __init__(self, arg1, arg2):
But we only actually pass it two arguments :
instance = OurClass('arg1', 'arg2')
Where has the extra argument come from ?
When we access attributes of an object we do it by name (or by reference). Here instance is a reference to our new object. We access the printargs method of the instance object using instance.printargs.
In order to access object attributes from within the __init__ method we need a reference to the object.
Whenever a method is called, a reference to the main object is passed as the first argument. By convention you always call this first argument to your methods self.
This means in the __init__ method we can do :
self.arg1 = arg1
self.arg2 = arg2
Here we are setting attributes on the object. You can verify this by doing the following :
instance = OurClass('arg1', 'arg2')
print instance.arg1
arg1
values like this are known as object attributes. Here the __init__ method sets the arg1 and arg2 attributes of the instance.
source: http://www.voidspace.org.uk/python/articles/OOP.shtml#the-init-method
note that self could actually be any valid python identifier. For example, we could just as easily write, from Chris B's example:
class A(object):
def __init__(foo):
foo.x = 'Hello'
def method_a(bar, foo):
print bar.x + ' ' + foo
and it would work exactly the same. It is however recommended to use self because other pythoners will recognize it more easily.
What does self do? What is it meant to be? Is it mandatory?
The first argument of every class method, including init, is always a reference to the current instance of the class. By convention, this argument is always named self. In the init method, self refers to the newly created object; in other class methods, it refers to the instance whose method was called.
Python doesn't force you on using "self". You can give it any name you want. But remember the first argument in a method definition is a reference to the object. Python adds the self argument to the list for you; you do not need to include it when you call the methods.
if you didn't provide self in init method then you will get an error
TypeError: __init___() takes no arguments (1 given)
What does the init method do? Why is it necessary? (etc.)
init is short for initialization. It is a constructor which gets called when you make an instance of the class and it is not necessary. But usually it our practice to write init method for setting default state of the object. If you are not willing to set any state of the object initially then you don't need to write this method.
__init__ is basically a function which will "initialize"/"activate" the properties of the class for a specific object, once created and matched to the corresponding class..
self represents that object which will inherit those properties.
Basically, you need to use the 'self' keyword when using a variable in multiple functions within the same class. As for init, it's used to setup default values incase no other functions from within that class are called.
The 'self' is a reference to the class instance
class foo:
def bar(self):
print "hi"
Now we can create an instance of foo and call the method on it, the self parameter is added by Python in this case:
f = foo()
f.bar()
But it can be passed in as well if the method call isn't in the context of an instance of the class, the code below does the same thing
f = foo()
foo.bar(f)
Interestingly the variable name 'self' is just a convention. The below definition will work exactly the same.. Having said that it is very strong convention which should be followed always, but it does say something about flexible nature of the language
class foo:
def bar(s):
print "hi"
Just a demo for the question.
class MyClass:
def __init__(self):
print('__init__ is the constructor for a class')
def __del__(self):
print('__del__ is the destructor for a class')
def __enter__(self):
print('__enter__ is for context manager')
return self
def __exit__(self, exc_type, exc_value, traceback):
print('__exit__ is for context manager')
def greeting(self):
print('hello python')
if __name__ == '__main__':
with MyClass() as mycls:
mycls.greeting()
$ python3 class.objects_instantiation.py
__init__ is the constructor for a class
__enter__ is for context manager
hello python
__exit__ is for context manager
__del__ is the destructor for a class
In this code:
class Cat:
def __init__(self, name):
self.name = name
def info(self):
print 'I am a cat and I am called', self.name
Here __init__ acts as a constructor for the class and when an object is instantiated, this function is called. self represents the instantiating object.
c = Cat('Kitty')
c.info()
The result of the above statements will be as follows:
I am a cat and I am called Kitty
# Source: Class and Instance Variables
# https://docs.python.org/2/tutorial/classes.html#class-and-instance-variables
class MyClass(object):
# class variable
my_CLS_var = 10
# sets "init'ial" state to objects/instances, use self argument
def __init__(self):
# self usage => instance variable (per object)
self.my_OBJ_var = 15
# also possible, class name is used => init class variable
MyClass.my_CLS_var = 20
def run_example_func():
# PRINTS 10 (class variable)
print MyClass.my_CLS_var
# executes __init__ for obj1 instance
# NOTE: __init__ changes class variable above
obj1 = MyClass()
# PRINTS 15 (instance variable)
print obj1.my_OBJ_var
# PRINTS 20 (class variable, changed value)
print MyClass.my_CLS_var
run_example_func()
Here, the guy has written pretty well and simple: https://www.jeffknupp.com/blog/2014/06/18/improve-your-python-python-classes-and-object-oriented-programming/
Read above link as a reference to this:
self? So what's with that self parameter to all of the Customer
methods? What is it? Why, it's the instance, of course! Put another
way, a method like withdraw defines the instructions for withdrawing
money from some abstract customer's account. Calling
jeff.withdraw(100.0) puts those instructions to use on the jeff
instance.
So when we say def withdraw(self, amount):, we're saying, "here's how
you withdraw money from a Customer object (which we'll call self) and
a dollar figure (which we'll call amount). self is the instance of the
Customer that withdraw is being called on. That's not me making
analogies, either. jeff.withdraw(100.0) is just shorthand for
Customer.withdraw(jeff, 100.0), which is perfectly valid (if not often
seen) code.
init self may make sense for other methods, but what about init? When we call init, we're in the process of creating an object, so how can there already be a self? Python allows us to extend
the self pattern to when objects are constructed as well, even though
it doesn't exactly fit. Just imagine that jeff = Customer('Jeff
Knupp', 1000.0) is the same as calling jeff = Customer(jeff, 'Jeff
Knupp', 1000.0); the jeff that's passed in is also made the result.
This is why when we call init, we initialize objects by saying
things like self.name = name. Remember, since self is the instance,
this is equivalent to saying jeff.name = name, which is the same as
jeff.name = 'Jeff Knupp. Similarly, self.balance = balance is the same
as jeff.balance = 1000.0. After these two lines, we consider the
Customer object "initialized" and ready for use.
Be careful what you __init__
After init has finished, the caller can rightly assume that the
object is ready to use. That is, after jeff = Customer('Jeff Knupp',
1000.0), we can start making deposit and withdraw calls on jeff; jeff is a fully-initialized object.
Python __init__ and self what do they do?
What does self do? What is it meant to be? Is it mandatory?
What does the __init__ method do? Why is it necessary? (etc.)
The example given is not correct, so let me create a correct example based on it:
class SomeObject(object):
def __init__(self, blah):
self.blah = blah
def method(self):
return self.blah
When we create an instance of the object, the __init__ is called to customize the object after it has been created. That is, when we call SomeObject with 'blah' below (which could be anything), it gets passed to the __init__ function as the argument, blah:
an_object = SomeObject('blah')
The self argument is the instance of SomeObject that will be assigned to an_object.
Later, we might want to call a method on this object:
an_object.method()
Doing the dotted lookup, that is, an_object.method, binds the instance to an instance of the function, and the method (as called above) is now a "bound" method - which means we do not need to explicitly pass the instance to the method call.
The method call gets the instance because it was bound on the dotted lookup, and when called, then executes whatever code it was programmed to perform.
The implicitly passed self argument is called self by convention. We could use any other legal Python name, but you will likely get tarred and feathered by other Python programmers if you change it to something else.
__init__ is a special method, documented in the Python datamodel documentation. It is called immediately after the instance is created (usually via __new__ - although __new__ is not required unless you are subclassing an immutable datatype).

Learning Python the Hard Way ex40, i dont understand the __init__ part [duplicate]

This question already has answers here:
What is the purpose of the `self` parameter? Why is it needed?
(26 answers)
Why do we use __init__ in Python classes?
(9 answers)
Closed 6 months ago.
I'm learning the Python programming language and I've came across something I don't fully understand.
In a method like:
def method(self, blah):
def __init__(?):
....
....
What does self do? What is it meant to be? Is it mandatory?
What does the __init__ method do? Why is it necessary? (etc.)
I think they might be OOP constructs, but I don't know very much.
In this code:
class A(object):
def __init__(self):
self.x = 'Hello'
def method_a(self, foo):
print self.x + ' ' + foo
... the self variable represents the instance of the object itself. Most object-oriented languages pass this as a hidden parameter to the methods defined on an object; Python does not. You have to declare it explicitly. When you create an instance of the A class and call its methods, it will be passed automatically, as in ...
a = A() # We do not pass any argument to the __init__ method
a.method_a('Sailor!') # We only pass a single argument
The __init__ method is roughly what represents a constructor in Python. When you call A() Python creates an object for you, and passes it as the first parameter to the __init__ method. Any additional parameters (e.g., A(24, 'Hello')) will also get passed as arguments--in this case causing an exception to be raised, since the constructor isn't expecting them.
Yep, you are right, these are oop constructs.
__init__ is the constructor for a class. The self parameter refers to the instance of the object (like this in C++).
class Point:
def __init__(self, x, y):
self._x = x
self._y = y
The __init__ method gets called after memory for the object is allocated:
x = Point(1,2)
It is important to use the self parameter inside an object's method if you want to persist the value with the object. If, for instance, you implement the __init__ method like this:
class Point:
def __init__(self, x, y):
_x = x
_y = y
Your x and y parameters would be stored in variables on the stack and would be discarded when the init method goes out of scope. Setting those variables as self._x and self._y sets those variables as members of the Point object (accessible for the lifetime of the object).
N.B. Some clarification of the use of the word "constructor" in this answer. Technically the responsibilities of a "constructor" are split over two methods in Python. Those methods are __new__ (responsible for allocating memory) and __init__ (as discussed here, responsible for initialising the newly created instance).
A brief illustrative example
In the hope it might help a little, here's a simple example I used to understand the difference between a variable declared inside a class, and a variable declared inside an __init__ function:
class MyClass(object):
i = 123
def __init__(self):
self.i = 345
a = MyClass()
print(a.i)
print(MyClass.i)
Output:
345
123
In short:
self as it suggests, refers to itself- the object which has called the method. That is, if you have N objects calling the method, then self.a will refer to a separate instance of the variable for each of the N objects. Imagine N copies of the variable a for each object
__init__ is what is called as a constructor in other OOP languages such as C++/Java. The basic idea is that it is a special method which is automatically called when an object of that Class is created
Class objects support two kinds of operations: attribute references and instantiation
Attribute references use the standard syntax used for all attribute references in Python: obj.name. Valid attribute names are all the names that were in the class’s namespace when the class object was created. So, if the class definition looked like this:
class MyClass:
"""A simple example class"""
i = 12345
def f(self):
return 'hello world'
then MyClass.i and MyClass.f are valid attribute references, returning an integer and a function object, respectively. Class attributes can also be assigned to, so you can change the value of MyClass.i by assignment. __doc__ is also a valid attribute, returning the docstring belonging to the class: "A simple example class".
Class instantiation uses function notation. Just pretend that the class object is a parameterless function that returns a new instance of the class. For example:
x = MyClass()
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
Taken from official documentation which helped me the most in the end.
Here is my example
class Bill():
def __init__(self,apples,figs,dates):
self.apples = apples
self.figs = figs
self.dates = dates
self.bill = apples + figs + dates
print ("Buy",self.apples,"apples", self.figs,"figs
and",self.dates,"dates.
Total fruitty bill is",self.bill," pieces of fruit :)")
When you create instance of class Bill:
purchase = Bill(5,6,7)
You get:
> Buy 5 apples 6 figs and 7 dates. Total fruitty bill is 18 pieces of
> fruit :)
__init__ does act like a constructor. You'll need to pass "self" to any class functions as the first argument if you want them to behave as non-static methods. "self" are instance variables for your class.
Try out this code. Hope it helps many C programmers like me to Learn Py.
#! /usr/bin/python2
class Person:
'''Doc - Inside Class '''
def __init__(self, name):
'''Doc - __init__ Constructor'''
self.n_name = name
def show(self, n1, n2):
'''Doc - Inside Show'''
print self.n_name
print 'Sum = ', (n1 + n2)
def __del__(self):
print 'Destructor Deleting object - ', self.n_name
p=Person('Jay')
p.show(2, 3)
print p.__doc__
print p.__init__.__doc__
print p.show.__doc__
Output:
Jay
Sum = 5
Doc - Inside Class
Doc - __init__ Constructor
Doc - Inside Show
Destructor Deleting object - Jay
Had trouble undestanding this myself. Even after reading the answers here.
To properly understand the __init__ method you need to understand self.
The self Parameter
The arguments accepted by the __init__ method are :
def __init__(self, arg1, arg2):
But we only actually pass it two arguments :
instance = OurClass('arg1', 'arg2')
Where has the extra argument come from ?
When we access attributes of an object we do it by name (or by reference). Here instance is a reference to our new object. We access the printargs method of the instance object using instance.printargs.
In order to access object attributes from within the __init__ method we need a reference to the object.
Whenever a method is called, a reference to the main object is passed as the first argument. By convention you always call this first argument to your methods self.
This means in the __init__ method we can do :
self.arg1 = arg1
self.arg2 = arg2
Here we are setting attributes on the object. You can verify this by doing the following :
instance = OurClass('arg1', 'arg2')
print instance.arg1
arg1
values like this are known as object attributes. Here the __init__ method sets the arg1 and arg2 attributes of the instance.
source: http://www.voidspace.org.uk/python/articles/OOP.shtml#the-init-method
note that self could actually be any valid python identifier. For example, we could just as easily write, from Chris B's example:
class A(object):
def __init__(foo):
foo.x = 'Hello'
def method_a(bar, foo):
print bar.x + ' ' + foo
and it would work exactly the same. It is however recommended to use self because other pythoners will recognize it more easily.
What does self do? What is it meant to be? Is it mandatory?
The first argument of every class method, including init, is always a reference to the current instance of the class. By convention, this argument is always named self. In the init method, self refers to the newly created object; in other class methods, it refers to the instance whose method was called.
Python doesn't force you on using "self". You can give it any name you want. But remember the first argument in a method definition is a reference to the object. Python adds the self argument to the list for you; you do not need to include it when you call the methods.
if you didn't provide self in init method then you will get an error
TypeError: __init___() takes no arguments (1 given)
What does the init method do? Why is it necessary? (etc.)
init is short for initialization. It is a constructor which gets called when you make an instance of the class and it is not necessary. But usually it our practice to write init method for setting default state of the object. If you are not willing to set any state of the object initially then you don't need to write this method.
__init__ is basically a function which will "initialize"/"activate" the properties of the class for a specific object, once created and matched to the corresponding class..
self represents that object which will inherit those properties.
Basically, you need to use the 'self' keyword when using a variable in multiple functions within the same class. As for init, it's used to setup default values incase no other functions from within that class are called.
The 'self' is a reference to the class instance
class foo:
def bar(self):
print "hi"
Now we can create an instance of foo and call the method on it, the self parameter is added by Python in this case:
f = foo()
f.bar()
But it can be passed in as well if the method call isn't in the context of an instance of the class, the code below does the same thing
f = foo()
foo.bar(f)
Interestingly the variable name 'self' is just a convention. The below definition will work exactly the same.. Having said that it is very strong convention which should be followed always, but it does say something about flexible nature of the language
class foo:
def bar(s):
print "hi"
Just a demo for the question.
class MyClass:
def __init__(self):
print('__init__ is the constructor for a class')
def __del__(self):
print('__del__ is the destructor for a class')
def __enter__(self):
print('__enter__ is for context manager')
return self
def __exit__(self, exc_type, exc_value, traceback):
print('__exit__ is for context manager')
def greeting(self):
print('hello python')
if __name__ == '__main__':
with MyClass() as mycls:
mycls.greeting()
$ python3 class.objects_instantiation.py
__init__ is the constructor for a class
__enter__ is for context manager
hello python
__exit__ is for context manager
__del__ is the destructor for a class
In this code:
class Cat:
def __init__(self, name):
self.name = name
def info(self):
print 'I am a cat and I am called', self.name
Here __init__ acts as a constructor for the class and when an object is instantiated, this function is called. self represents the instantiating object.
c = Cat('Kitty')
c.info()
The result of the above statements will be as follows:
I am a cat and I am called Kitty
# Source: Class and Instance Variables
# https://docs.python.org/2/tutorial/classes.html#class-and-instance-variables
class MyClass(object):
# class variable
my_CLS_var = 10
# sets "init'ial" state to objects/instances, use self argument
def __init__(self):
# self usage => instance variable (per object)
self.my_OBJ_var = 15
# also possible, class name is used => init class variable
MyClass.my_CLS_var = 20
def run_example_func():
# PRINTS 10 (class variable)
print MyClass.my_CLS_var
# executes __init__ for obj1 instance
# NOTE: __init__ changes class variable above
obj1 = MyClass()
# PRINTS 15 (instance variable)
print obj1.my_OBJ_var
# PRINTS 20 (class variable, changed value)
print MyClass.my_CLS_var
run_example_func()
Here, the guy has written pretty well and simple: https://www.jeffknupp.com/blog/2014/06/18/improve-your-python-python-classes-and-object-oriented-programming/
Read above link as a reference to this:
self? So what's with that self parameter to all of the Customer
methods? What is it? Why, it's the instance, of course! Put another
way, a method like withdraw defines the instructions for withdrawing
money from some abstract customer's account. Calling
jeff.withdraw(100.0) puts those instructions to use on the jeff
instance.
So when we say def withdraw(self, amount):, we're saying, "here's how
you withdraw money from a Customer object (which we'll call self) and
a dollar figure (which we'll call amount). self is the instance of the
Customer that withdraw is being called on. That's not me making
analogies, either. jeff.withdraw(100.0) is just shorthand for
Customer.withdraw(jeff, 100.0), which is perfectly valid (if not often
seen) code.
init self may make sense for other methods, but what about init? When we call init, we're in the process of creating an object, so how can there already be a self? Python allows us to extend
the self pattern to when objects are constructed as well, even though
it doesn't exactly fit. Just imagine that jeff = Customer('Jeff
Knupp', 1000.0) is the same as calling jeff = Customer(jeff, 'Jeff
Knupp', 1000.0); the jeff that's passed in is also made the result.
This is why when we call init, we initialize objects by saying
things like self.name = name. Remember, since self is the instance,
this is equivalent to saying jeff.name = name, which is the same as
jeff.name = 'Jeff Knupp. Similarly, self.balance = balance is the same
as jeff.balance = 1000.0. After these two lines, we consider the
Customer object "initialized" and ready for use.
Be careful what you __init__
After init has finished, the caller can rightly assume that the
object is ready to use. That is, after jeff = Customer('Jeff Knupp',
1000.0), we can start making deposit and withdraw calls on jeff; jeff is a fully-initialized object.
Python __init__ and self what do they do?
What does self do? What is it meant to be? Is it mandatory?
What does the __init__ method do? Why is it necessary? (etc.)
The example given is not correct, so let me create a correct example based on it:
class SomeObject(object):
def __init__(self, blah):
self.blah = blah
def method(self):
return self.blah
When we create an instance of the object, the __init__ is called to customize the object after it has been created. That is, when we call SomeObject with 'blah' below (which could be anything), it gets passed to the __init__ function as the argument, blah:
an_object = SomeObject('blah')
The self argument is the instance of SomeObject that will be assigned to an_object.
Later, we might want to call a method on this object:
an_object.method()
Doing the dotted lookup, that is, an_object.method, binds the instance to an instance of the function, and the method (as called above) is now a "bound" method - which means we do not need to explicitly pass the instance to the method call.
The method call gets the instance because it was bound on the dotted lookup, and when called, then executes whatever code it was programmed to perform.
The implicitly passed self argument is called self by convention. We could use any other legal Python name, but you will likely get tarred and feathered by other Python programmers if you change it to something else.
__init__ is a special method, documented in the Python datamodel documentation. It is called immediately after the instance is created (usually via __new__ - although __new__ is not required unless you are subclassing an immutable datatype).

Python Class Inheritance, __init__ and cls

The desired output of the code is that I have a class variable Team.stuff which has one entry holding the b instance, and the Player.stuff variable should be empty. Instead I get an error...
class Player:
stuff=[]
def __init__(self):
cls.stuff.append(self)
class Team(Player):
def __init__(self):
super(Team, self).__init__()
b=Team()
ERROR
cls.stuff.append(self)
NameError: global name 'cls' is not defined
I could pass the cls variable in the Team.__init__(), but I'm not sure if that is the "correct" way, and more importantly the Player.__init__() would need a class variable, and I'm not sure on the syntax on how to do that.
class Player(object):
stuff=[]
def __init__(self):
self.stuff.append(self)
class Team(Player):
def __init__(self):
super(Team, self).__init__()
b = Team()
print(Team.stuff)
prints (something like)
[<__main__.Team object at 0xb7519dec>]
Remember that cls is not a keyword in Python. Rather it is a convention for the first argument of a method when that method is supposed to be a class function and not one that is called on a particular instance of something.
If you want stuff to be an attribute of a function, you have to define it like so: self.stuff=[].
And then, when you refer to it in methods, do it with the self keyword as well: self.stuff.append(..).
The reason for this is that cls in your __init__ function is not defined. The argument names self and cls are just naming conventions. In reality, you could name them foo and bar for an instance and a class method respectively, and foo would point at the class instance whilst bar would point a the class itself.
Moreover, self.stuff would be valid as when searching for an attribute, if an object does not have an attribute of such name in it's __dict__ dictionary, the __dict__ of it's class is looked instead. If both are missing, further lookups are done according to mro order.
Notice that, once you set an attribute stuff on an object instance, it will shadow the class definition.
Explained by example:
class MyClassA(object):
stuff = []
class MyClassB(MyClassA):
def __init__(foobar):
# `foobar` points at the instance of `MyClassB` class.
# MyClassA.stuff gets `1` object appended to it.
foobar.stuff.append(1)
# This should print '[1]'
print foobar.stuff
# `MyClassB` object gets a _new_ attribute named `stuff` pointing at a list.
foobar.stuff = []
# This should print '[]'.
print foobar.stuff

Categories

Resources