Python3: Object won't take parameters - python

I am working through reviewing a video on Lynda and sometimes instead of just running the provided code I typed it myself.
The instructor's code works, but mine doesn't.
It returns "object does not take parameters".
Mine is on top, his is on the bottom.
Can anyone explain?
class Fibonnacci():
def _init_(self, a, b):
self.a = a
self.b = b
def series(self):
while(True):
yield(self.b)
self.a, self.b = self.b, self.a + self.b
f = Fibonnacci(0, 1)
for r in f.series():
if r > 100: break
print(r, end=' ')
class Fibonacci():
def __init__(self, a, b):
self.a = a
self.b = b
def series(self):
while(True):
yield(self.b)
self.a, self.b = self.b, self.a + self.b
f = Fibonacci(0, 1)
for r in f.series():
if r > 100: break
print(r, end=' ')

The reason is the init function in the class. The way this method works as seen here to let an object be assigned parameters when initialized. I am sure you understand this, but the simple error you made was that it requires 2 underscores on either side to take effect. Like this: __init__. That is the difference between your two codes.
class Fibonnacci():
def _init_(self, a, b): #<-- the error is here, should be __init__()
self.a = a
self.b = b
def series(self):
while(True):
yield(self.b)
self.a, self.b = self.b, self.a + self.b
f = Fibonnacci(0, 1)
for r in f.series():
if r > 100: break
print(r, end=' ')

Related

Which one is faster, class function using self parameters or class function using argument parameters?

I would like to know if it is faster using self. variables in a class function than using function argument parameters. I tried with timeit but it gives the same time. So I'm guessing those are equivalent but I don't know if I'm doing it right. Is there another option to confirm it?
from numba import float64
from numba.experimental import jitclass
import timeit
spec=[
('a' ,float64),
('b' ,float64),
('c' ,float64),
('d' ,float64),
]
#jitclass(spec)
class MyClass:
def __init__(self):
self.a = 1
self.b = 1
self.c = 1
self.d = 1
def add_param(self, a, b, c):
return a + b + c
def add_self(self):
self.d = self.a + self.b +self.c
def run_param(self):
self.d = self.add_param(self.a, self.b, self.c)
def run_self(self):
self.add_self()
C = MyClass()
print(timeit.Timer(C.run_param).timeit(1000000))
print(timeit.Timer(C.run_self).timeit(1000000))

How can I sum up the values of two functions?

I have two classes. In class B I want to change the values of the variables in class A, which are defined in functions: some and anyt, with the help of the functions in class B: frog and lion.
For example, I multiply by 2 or 3..
I get the Error:
File "E:/Python/Akustik/Akustik/Test.py", line 20
A.some(a,b,c) = A.some(a,b,c)* 2
^
SyntaxError: can't assign to function call
I know what does that mean, but i can't dispense with the functions in class A and B, does anybody have a tip?
here is the code:
class A:
def some(self,a, b, c):
self.a = 4
self.b = 2
self.c = 3
def anyt(self, p, g, f):
self.p = 5
self.g = 8
self.f = 1
class B(A):
def frog(self):
A.some(a,b,c) = A.some(a,b,c)* 2
def lion(self):
A.anyt(p,g,f)= A.anyt(p,g,f) * 3
You cannot assign the value of an expression to a function call. In your case if you want to change the value of variables a, b, c, p, q, r. You will have to do something like this.
class A:
def __init__(self):
self.a = 1
self.b = 1
self.c = 1
self.p = 1
self.q = 1
self.r = 1
def some(self,a, b, c):
self.a = a
self.b = b
self.c = c
def anyt(self, p, g, f):
self.p = p
self.g = g
self.f = f
class B(A):
def frog(self):
self.some(self.a*2, self.b*2, self.c*2)
def lion(self):
self.anyt(self.p*3, self.g*3, self.f*3)
b = B()
b.frog()
print(b.c)
# Prints current value of c
This ensures the corresponding variable values change.

Python __init__ without executing a variable

I am looking for a way to init a variable in a class. This variable is dependent of other variables that I init in the same class too. Here is an example.
Class class():
def __init__()
self.a = None
self.b = None
self.sum = self.a + self.b
def change_a_and_b()
self.a = input("a = ")
self.b = input("b = ")
def print_sum()
print(self.sum)
This is a simplified example. In my program self.sum is a complicated calculation which I want to use like a "shortcut" so I don't need to write it a lot of times. The problem is I input the variables after the init function. I just don't want to execute self.sum = self.a + self.b when I run __init__. I think there is a do_thing parameter, but I don't know how to use it.
You can make sum a property:
class my_class():
def __init__(self)
self.a = None
self.b = None
#property
def sum(self):
return self.a + self.b
def change_a_and_b(self)
self.a = input("a = ")
self.b = input("b = ")
def print_sum(self)
print(self.sum)

NameError: name 'addition' is not defined

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()

How to access variable outside class in my example?

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

Categories

Resources