I am trying to access a variable from static method to outside.
class Hello():
def __init__(self, a, b):
self.a = a
self.b = b
self.c = 0
#staticmethod
def add():
d = 10
print("hello", d)
How to access variable 'd' in outside of static method?
First of all, you don't need #staticmethod for just d = 10. You can assign it outside of the constructor (as mentioned below). And then to access that variable, you can try, CLASS_NAME.VARIABLE_NAME.
class Hello():
d = 10
def __init__(self, a, b):
self.a = a
self.b = b
self.c = 0
print("hello", Hello.d)
Related
I would like to use functions based on strings passed in argument as below. The code gives the following error: AttributeError: 'Obj' object has no attribute 'funA'
The functions funA and funB are defined within fun because they are only used within fun and nowhere else
class Obj(object):
def __init__(self):
self.A = 2
self.B = 3
def fun(self, ar):
def funA(self):
print self.A
def funB(self):
x = self.B + 4
print self.B
for letter in ar:
name = 'fun' + letter
getattr(self, name)()
obj_instance = Obj()
obj_instance.fun(['A', 'B'])
As an alternative you can try the following code. It gives you the opportunity to check whether a function exists or not.
class Obj(object):
def __init__(self):
self.A = 2
self.B = 3
def fun(self, ar):
def funA():
print self.A
def funB():
x = self.B + 4
print self.B
print x
for letter in ar:
name = 'fun' + letter
if name in locals():
locals()[name]()
obj_instance = Obj()
obj_instance.fun(['A', 'B'])
I wouldn't recommend it, but if you really need it, then you can do it this way:
class Obj(object):
def __init__(self):
self.A = 2
self.B = 3
def fun(self, ar):
def funA(self):
print self.A
def funB(self):
x = self.B + 4
print self.B
for letter in ar:
name = 'fun' + letter + '()'
exec(name)
obj_instance=Obj()
obj_instance.fun(['A', 'B'])
exec() executes any string that you put in parentheses as if it was python code.
I am getting NameError: name 'addition' is not defined while running following code
class Arithmetic:
def __init__(self, a, b):
self.a = a
self.b = b
def addition(self):
c = a + b
print"%d" %c
def subtraction(self):
c=a-b
print "%d" % c
add = addition(5, 4)
add.addition()
If you want to use your 'addition' method, you first need to instantiate an Arithmetic() object and use dot notation to call their functions. Make sure you properly indent your code because not only is it breaking a lot of PEP 8 rules but it just looks plain messy. In your first definition, don't forget you have to type __init__ not init. Here's the code which should be applied:
class Arithmetic(object):
def __init__(self, a, b):
self.a = a
self.b = b
def addition(self):
c = self.a + self.b
print c
def subtraction(self):
c = self.a - self.b
print c
a = Arithmetic(5, 4)
a.addition()
a.subtraction()
You first have to create object of class and then you can access class function.
Try this:
a = Arithmatic()
a.addition(5,4)
Check out this piece of code:
class Arithmetic():
def init(self, a, b):
self.a = a
self.b = b
def addition(self):
c = self.a + self.b
print"addition %d" %c
def subtraction(self):
c = self.a - self.b
print"substraction %d" %c
obj = Arithmetic()
obj.init(5, 4)
obj.addition()
obj.subtraction()
class testing():
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
def house(self):
d = self.a+self.b+self.c
print d
module="hello"
p = testing(1, 2, 3)
p.house()
How do I access module variable from within my testing class? I know I could just add it as a parameter to the class constructor by doing:
p=testing(1,2,3,module)
But I don't want to do that unless I have to. What other ways can I access module variable from inside the testing class?
You simply reference it; you don't need any special global permission to access it. This isn't the best way, but since you haven't described your application and modularity requirements, about all we can do right now is to solve your immediate problem.
By the way, your a, b, c references are incorrect. See below.
class testing():
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
self.greeting = module
def house(self):
d = self.a + self.b + self.c
print d
print self.greeting
module="hello"
p = testing(1, 2, 3)
p.house()
Output:
6
hello
You could use globals(). But I'm not sure if this is good idea at all.
class testing():
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
def house(self):
print(globals()['module'])
d = self.a + self.b + self.c
print(d)
module = 'here'
t = testing(1, 2, 3)
t.house()
Output:
# here
# 6
Maybe I don't understand the question, it already works since the global variable "module" is defined before you instantiated the class.
class testing():
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
def house(self):
d = self.a+self.b+self.c
print module
print d
module="hello"
p = testing(1, 2, 3)
p.house()
outputs:
hello
6
Have written a simple code like this:
class Operations:
#global a,b
a=1
b=2
def __init__(self):
print(self,"object has been created")
def add(self):
#a = 2
#b = 3
return a+b
obj1=Operations()
sum=obj1.add()
print(sum).
when i run this code, am getting this error NameError: name 'a' is not defined.
can you please explain why variables a and b are not accessible in the method 'add' which is defined in the same class?
Note:when am declaring variables as a global, am able to access the variables inside the 'add' method.
You need to use the self keyword.
What does self do?
a = 1
class Operations:
a = 2
def fun1(self):
return a
def fun2(self):
return self.a
obj = Operations()
print(obj.fun1())
print(obj.fun2())
Output:
1
2
Solution for you case:
class Operations:
a=1
b=2
def __init__(self):
print(self,"object has been created")
def add(self):
return self.a + self.b
obj1=Operations()
print(obj1.add())
Output:
<__main__.Operations object at 0x100663588> object has been created
3
Use the class reference
Value= self.a + self.b
Is this the answer that you need? if you're writing a class, use self.value instead global value:
class Operations:
def __init__(self):
self.a = 1
self.b = 2
print(self, "object has been created")
def add(self):
return self.a + self.b
obj1 = Operations()
print(obj1.add())
I am wondering if it is possible to initialize a class stored in a variable. For example:
class cl():
def __init(self, a):
self.a = a
return
def print(self):
print(self.a)
return a
In[27]: c = cl
In[28]: c
Out[28]: __main__.cl
and now I want to initialize cl with value of a=3
class cl():
def __init__(self, a):
self.a = a
def print_a(self):
print(self.a)
c = cl
c(3)
c.print_a()
>>> 3
Yes it is totally possible. When you do c = c1 it creates c as an exact alias of c1.
You then do
obj_from_c1 = c1()
obj_from_c c()