Beruflich Dokumente
Kultur Dokumente
Global namespace
a=22
b=a+1
a and b are globally defined and will work for any functions that don't have any
value set to a and b locally.
Built-in namespace
names such as max(), min(), list, etc are are created at interpreter startup and
are never deleted.
-----------------------------------------------------------------------------------
-------
3.
-----------------------------------------------------------------------------------
-------
4.
LEGB= LOCAL ENCLOSED GLOBAL BUILT-IN
Whenever a name is encountered, it first looks for the name in the LOCAL
namespace.
If the name is not found there, it searches in all enclosing functions.
If not, then it searches for the name in Global namespace and finally
searches in the BUILT-IN namespace.
5.
Objects of a class can be mutable as you can directly assign some new value to that
object.
And object of a class can be internally mutable as well.
You can use built-in methods like "setattr()" or "delattr()" to change,
reassign or delete values. You can also add methods.
-----------------------------------------------------------------------------------
-------
6.
If a class doesn't contain any statements i.e. it's a class without any attributes,
then a keyword 'pass' is given within the body of the class.
Giving this keyword means that the class doesn't define any methods or attributes.
But a class cannot be empty, that's why 'pass' is given.
-----------------------------------------------------------------------------------
-------
7.
__init__ is a constructor, a special method that is used to initialize the data
members of a class.
__init__ is a built in method in Python. It's teh first method defined for the
class and is the first piece of code
executed in a newly created instance of this class. It is called as soon as an
instance is created.
Example:
class CLASS:
def __init__(self,name="XYZ") #default name is xyz
self.name=name
A=CLASS("classname")
Here, as soon as the instantiation of the object A takes place, init is called
and the arguement "classname" is assigned to the 'name' value of instance.
-----------------------------------------------------------------------------------
-------
8.
Importance of self
'self' is given as an extra arguement before any other arguement during the def of
a method. This 'self' is used to refer the instance. One need not give any
parameter for this arguement. It is an instance identificator and is required so
that the statements within the method can have automatic access to current instance
attributes. It is not a reserved keyword but it is strongly recommended to use this
word as it is always convinient to use the convention because it makes the program
more readable.
-----------------------------------------------------------------------------------
-------
9.
Class attributes are attributes for a class. For a defined class attribute if its
value is changed using any instance of the same class, it's value is changed for
all the instances for the class.
Instance attributes are attributes of individual instances.
These can be changed specifically for a given instance. Instance attributes are
specific properties for an object.
Example:
class A:
a=[]
def __init__(self):
self.b=[]
obj1=A()
obj2=A()
print obj1.a
print obj2.a
print obj1.b
print obj2.b