Here is the question of a python code asked on InfyTQ mock test.
class classOne:
__var_one = 1001
def __init__(self,var_two):
self.__var_two = var_two
self.__var_five = 5
def method_one(self):
var_four = 50
self.__var_five = ClassOne.__var_one + self.__var_two + var_four
Now, I want to ask if the variable
self.__var_five of function method_one should be considered a new instance variable or not?
Because there is already a self.__var_five in __init__ function.
Also,
I learned the concept of global,local,static and instance variable from given below code.
Is it correct?
#global, local, static, instance variable.
#global variable are defined at the top of program or defined using keyword:global
global global_var1 = 0
global_var2 = 1
def local_variable:
#local variable are defined inside of a function.
local_var1 = 2
class static_instance:
#static/classs variable are defined inside of a class.
static_var1 = 3
def __init__(self):
#all variables defined in the function of a class starting with self.
self.instance_var1 = 4
def static(self):
self.instance_var2 = 5
local_var2 = 6 #local variable as it is in a function.
static_var2 = 6
It's the same instance variable (called an attribute in Python). method_one is simply updating its value.
Most of your understandings in the second code block are correct. However, when a method does:
self.static_var1 = 4
it creates an instance attribute named static_var1. This is independent of the class attribute that was declared outside the methods, which is shared among all instances that don't reassign it. But since you do the assignment in the __init__() method, all instances get their own attribute. The only way to access the static value would be with static_instance.static_var1.
Related
I'm making a python app and I have a class with a function that changes a variable created after the class. I can't put that variable before the class because that variable refers to variables inside the class and I end up with a paradox. It looks something like this:
class LeClass:
def __init__(self):
#Casual__init__Stuff
def LeFunction(self):
A = 1
A = LeClass()
Anyone got a solution? Thanks!
Declare the variable A global before assigning it from within your class code:
global A
A = 1
Otherwise, A will be a local variable that goes out of scope (is not accessible anymore) after the method returns.
Just tell python to look for the variable outside the class first when you try to use that variable.
Here is the code :
class LeClass:
def __init__(self):
pass
#Casual__init__Stuff
def LeFunction(self):
global A
A = 1
A = LeClass()
print("First Print :", A)
A.LeFunction()
print("Second Print :", A)
And here is the output of the code :
First Print : <__main__.LeClass object at 0x7f2ba3f1bb38>
Second Print : 1
I have simple class written
class Test:
stat = 0
def __init__(self):
self.inst = 10
def printa(self):
print Test.stat
print self.inst
Now I've created two object of this class
$ a = Test()
$ b = Test()
When I say a.printa() or b.printa() it outputs 0 10 which is understandable.
But when I say
$ a.stat = 2
$ print a.stat
It'll output
2
But when I say a.printa()
It'll output
1
10
What's the difference between saying objInstance.staticVar and ClassName.staticVar?? What it is doing internally?
Unless you do something to change how attribute assignment works (with __setattr__ or descriptors), assigning to some_object.some_attribute always assigns to an instance attribute, even if there was already a class attribute with that name.
Thus, when you do
a = Test()
a.stat is the class attribute. But after you do
a.stat = 2
a.stat now refers to the instance attribute. The class attribute is unchanged.
By
1
10
I assume you mean
0
10
since the class variable stat was never changed.
To answer your question, it does this because you added an instance member variable stat to the a object. Your objInstance.staticVar isn't a static variable at all, it's that new variable you added.
I understand in Python a class can have a class variable (shared among all objects), and also a unique object variable (which is uniquely assignable from object to object).
However, why do I get a 'object has no attribute 'nObjVar' error, when I try to access that object's variable from another function?
class TestClass:
nClassVar1 = 0 #Class Var shared across all objects
def __init__(self, defaultInitVal = 0):
nObjVar = defaultInitVal #Object Var Only
def MyFuncMult(self):
result = nObjVar * 10;
return ( result )
The reason is because you are defining it as a local variable in the init, if you want it to be a member of the object you need to type
self.nObjVar
this will set it to a member
To make a variable available to all methods in a class, save it in the self namespace.
class TestClass:
nClassVar1 = 0 #Class Var shared across all objects
def __init__(self, defaultInitVal = 0):
self.nObjVar = defaultInitVal #Object Var Only
def MyFuncMult(self):
result = self.nObjVar * 10;
return ( result )
Try self.nObjVar to reference the variable of the instance.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Python: Difference between class and instance attributes
I'm trying to get my head around OOP in Python and I'm a bit confused when it comes to declare variables within a class. Should I declare them inside of the __init__ procedure or outside it? What's the difference?
The following code works just fine:
# Declaring variables within __init__
class MyClass:
def __init__(self):
country = ""
city = ""
def information(self):
print "Hi! I'm from %s, (%s)"%(self.city,self.country)
me = MyClass()
me.country = "Spain"
me.city = "Barcelona"
me.information()
But declaring the variables outside of the __init__ procedure also works:
# Declaring variables outside of __init__
class MyClass:
country = ""
city = ""
def information(self):
print "Hi! I'm from %s, (%s)"%(self.city,self.country)
me = MyClass()
me.country = "Spain"
me.city = "Barcelona"
me.information()
In your first example you are defining instance attributes. In the second, class attributes.
Class attributes are shared between all instances of that class, where as instance attributes are "owned" by that particular instance.
Difference by example
To understand the differences let's use an example.
We'll define a class with instance attributes:
class MyClassOne:
def __init__(self):
self.country = "Spain"
self.city = "Barcelona"
self.things = []
And one with class attributes:
class MyClassTwo:
country = "Spain"
city = "Barcelona"
things = []
And a function that prints out information about one of these objects:
def information(obj):
print "I'm from {0}, ({1}). I own: {2}".format(
obj.city, obj.country, ','.join(obj.things))
Let's create 2 MyClassOne objects and change one to be Milan, and give Milan "something":
foo1 = MyClassOne()
bar1 = MyClassOne()
foo1.city = "Milan"
foo1.country = "Italy"
foo1.things.append("Something")
When we call information() on the foo1 and bar1 we get the values you'd expect:
>>> information(foo1)
I'm from Milan, (Italy). I own: Something
>>> information(bar1)
I'm from Barcelona, (Spain). I own:
However, if we were to do exactly the same thing, but using instances of MyClassTwo you'll see that the class attributes are shared between instances.
foo2 = MyClassTwo()
bar2 = MyClassTwo()
foo2.city = "Milan"
foo2.country = "Italy"
foo2.things.append("Something")
And then call information()...
>>> information(foo2)
I'm from Milan, (Italy). I own: Something
>>> information(bar2)
I'm from Barcelona, (Spain). I own: Something
So as you can see - things is being shared between the instances. things is a reference to a list that each instance has access to. So if you append to things from any instance that same list will be seen by all other instances.
The reason you don't see this behaviour in the string variables is because you are actually assigning a new variable to an instance. In this case that reference is "owned" by the instance and not shared at the class level. To illustrate let's assign a new list to things for bar2:
bar2.things = []
This results in:
>>> information(foo2)
I'm from Milan, (Italy). I own: Something
>>> information(bar2)
I'm from Barcelona, (Spain). I own:
You're two versions of the code are very different. In python, you have 2 distinct entities: classes and class instances. An instance is what is created when you do:
new_instance = my_class()
You can bind attributes to an instance within __init__ via self (self is the new instance).
class MyClass(object):
def __init__(self):
self.country = "" #every instance will have a `country` attribute initialized to ""
There's nothing terribly special about self and __init__. self is the customary name that is used to represent the instance that gets passed to every method (by default).
a.method() #-> Inside the class where `method` is defined, `a` gets passed in as `self`
The only thing special here is that __init__ gets called when the class is constructed:
a = MyClass() #implicitly calls `__init__`
You can also bind attributes to the class (putting it outside __init__):
class MyClass(object):
country = "" #This attribute is a class attribute.
At any point, you can bind a new attribute to an instance simply by:
my_instance = MyClass()
my_instance.attribute = something
Or a new attribute to a class via:
MyClass.attribute = something
Now it gets interesting. If an instance doesn't have a requested attribute, then python looks at the class for the attribute and returns it (if it is there). So, class attributes are a way for all instances of a class to share a piece of data.
Consider:
def MyClass(object):
cls_attr = []
def __init__(self):
self.inst_attr = []
a = MyClass()
a.inst_attr.append('a added this')
a.cls_attr.append('a added this to class')
b = MyClass()
print (b.inst_attr) # [] <- empty list, changes to `a` don't affect this.
print (b.cls_attr) # ['a added this to class'] <- Stuff added by `a`!
print (a.inst_attr) #['a added this']
When you define a variable in class scope (outside any method), it becomes a class attribute. When you define a value in method scope, it becomes a method local variable. If you assign a value to an attribute of self (or any other label referencing an object), it becomes (or modifies) an instance attribute.
Can you please clarify about the perimeter variable in the below class.
I understand that the self.vertices is to a particular instance. Since perimeter is not defined with self, does that mean its a class variable here? Then is it not common to all the instances?
Is it not the right way to code the perimeter as self.perimeter, so its aptly declared to each instance?
This code is from a book.
Polygon.py
import math
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def distance(self, p2):
return math.sqrt((self.x-p2.x)**2 + (self.y-p2.y)**2)
class Polygon:
def __init__(self):
self.vertices = []
def add_point(self, point):
self.vertices.append((point))
def perimeter(self):
perimeter = 0
points = self.vertices + [self.vertices[0]]
for i in range(len(self.vertices)):
perimeter += points[i].distance(points[i+1])
return perimeter
>>> square = Polygon()
>>> square.add_point(Point(1,1))
>>> square.add_point(Point(1,2))
>>> square.add_point(Point(2,2))
>>> square.add_point(Point(2,1))
>>> square.perimeter()
4.0
New type
import math
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def distance(self, p2):
return math.sqrt((self.x-p2.x)**2 + (self.y-p2.y)**2)
class Polygon:
def __init__(self):
self.vertices = []
def add_point(self, point):
self.vertices.append((point))
def perimetermethod(self):
self.perimeter = 0
points = self.vertices + [self.vertices[0]]
for i in range(len(self.vertices)):
self.perimeter += points[i].distance(points[i+1])
return self.perimeter
if __name__=='__main__':
p1 = Polygon()
p1.add_point(Point(1,1))
p1.add_point(Point(1,2))
p1.add_point(Point(2,2))
p1.add_point(Point(2,1))
print(p1.perimetermethod())
Assigning a new variable with some_name = ... always creates the variable in the innermost enclosing scope (unless global or nonlocal are in play, but they're not relevant here). Assigning a new attribute name on an object creates the attribute on that object.
So self.foo = 1 assigns an attribute named foo on the object currently referred to by self. Conventionally, the name self is used as the first parameter to a method, which receives the object on which the method was invoked. So "defining a variable with self" isn't anything special; it's just the ordinary rules about assigning to attributes on existing object. Any attributes that exist in instance object itself obviously have to be specific to that instance.
perimeter = 0 inside the perimeter method of the Polygon class creates a variable in the innermost enclosing scope. That's the perimeter method, so it creates a local variable. A local variable only exists for the duration of the function call, so it's neither a class variable nor an instance variable. You can't access it from anywhere except within the scope of that particular method (and it has a new completely independent value on each invocation), so it can't be common to all the instances. But neither can you access a different value for it on each particular instance, so it's not an instance variable either.
If you had perimeter = 0 outside a method, in the class block itself, then the innermost enclosing scope would be the class block. That would create a "class variable", which is just an attribute on the class object. If an attribute is on a class, then obviously it can't be specific to any instance, because there's only one class but there can be any number of instances. As an aside, this is exactly what the __init__, add_point, and perimeter methods of the Polygon class are; they were assigned (with a def statement) in a class block, so they became attributes of the class object.
Summary:
self.foo = 1 is assigning to an attribute on the object currently referenced by self (this is usually the "current instance")
foo = 1 in a class block is creating a class attribute of the class being defined
foo = 1 in a def block is creating a local variable of the function being defined
But you shouldn't really memorise it that way. They're just special cases of:
Assigning to a dotted name like foo.bar.baz = 1 is writing to an attribute of an object
Assigning to a simple name like foo = 1 is writing to a variable in the innermost enclosing scope
No, that means that it's local. Also, you don't want to use self.perimeter since it will shadow the method with the same name.
In your code, there are two things called perimeter. One is a method on the Polygon class. The other is a local variable inside that method. There are no class attributes in your code.