Class A has functionA has if. And functionA is repeated by 100 time in out.
In this case, it looks non scene to caluculate if self.z.... each time in called by for in out.
Class A():
def __ini__(self, z)
self.z = z
def functionA(self, a):
if self.z == 1:
b = zFunc1 * a
elif self.z == 2:
b = zFunc2 * a
return b
def out(self):
for i in range(100):
print(self.functionA(i))
So I'm looking for the way to fix functionA in __ini__ like
Class A():
def __ini__(self, z):
self.z = z
functionA < = FIX by (self.z = 2)
"""
def functionA(self, a):
b = zFunc2 * a
reuturn b
"""
Is there solution?
Just make zFunc* another class attribute decided in __init__:
class A:
def __init__(self, z):
self.z = z
self.z_func = zFunc1 if z == 1 else zFunc2
def function_a(self, a):
return self.z_func * a
In fact, in this example you don't even need self.z as an attribute at all.
Related
I am having trouble getting a test to pass. When I run the code it seems to work find, but in pytest it fails:
desk.py
class Dimension:
x = 0
y = 0
z = 0
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
class Desk:
def __init__(self, dimension):
self.dimension = dimension
#property
def dimension(self):
return self.__dimension
#dimension.setter
def dimension(self, d):
s = d.split(".")
self.__dimension = Dimension(int(s[0]), int(s[1]), int(s[2]))
#property
def is_large(self):
if self.dimension.x > 100:
return True
return False
test_desk.py
...
def test_is_large():
desk = Desk("5.5.5")
assert desk.is_large == False
...
I get AttributeError: 'str' object has no attribute 'x'
If I change to getter and setter methods it works find, but I would like to use decorators.
UPDATE:
I used python3 -m pytest to run pytest using python3 and it works fine
It's the double underscore of self.__dimension. Read this: What is the difference in python attributes with underscore in front and back
And this: How to access "__" (double underscore) variables in methods added to a class
Change self.__dimension to self._dimension and it will make.
EDIT: Not the underscore. Your code works perfectly in Python3. In Python2 I made it work this way:
class Dimension:
x = 0
y = 0
z = 0
def __init__(self, x, y, z):
self.x = 0
self.y = 0
self.z = 0
class Desk:
def __init__(self, dimension):
s = dimension.split(".")
self.__dimension = Dimension(int(s[0]), int(s[1]), int(s[2]))
#property
def dimension(self):
return self.__dimension
#dimension.setter
def dimension(self, d):
s = d.split(".")
self.__dimension = Dimension(int(s[0]), int(s[1]), int(s[2]))
#property
def is_large(self):
if self.dimension.x > 100:
return True
return False
Python does this:
t = (1, 2)
x, y = t
# x = 1
# y = 2
How can I implement my class so to do
class myClass():
def __init__(self, a, b):
self.a = a
self.b = b
mc = myClass(1, 2)
x, y = mc
# x = 1
# y = 2
Is there a magic function I could implement in order to achieve this?
You need to make your class iterable. Do this by adding the __iter__ method to it.
class myClass():
def __init__(self, a, b):
self.a = a
self.b = b
def __iter__(self):
return iter([self.a, self.b])
mc = myClass(1, 2)
x, y = mc
print(x, y)
Output:
1 2
If your class doesn't do much else, you might prefer to use a named tuple:
from collections import namedtuple
MyClass = namedtuple('MyClass', 'a b')
mc = MyClass(1, 2)
print(mc.a, mc.b) # -> 1 2
x, y = mc
print(x, y) # -> 1 2
BTW, style note: Class names should be UpperCamelCase.
I am trying to make an attribution of an attribution in python.
Is there a way to do so:
class Foo():
def __init__(self, x, y):
self.x = x
self.x.y = y
I have no idea how to do it, I checked for some examples. But I have not found any example similar.
Not exactly clear what you need you can do something like this (for example):
class Bar(object):
def __init__(self, y=None):
self.y = y
class Foo(object):
def __init__(self, x, y):
self.x = x
self.x.y = y
if __name__ == '__main__':
x = Bar()
y = 1
foo = Foo(x,y)
Say I have two functions
def do1(x, y):
return x + y
def do2(x, y):
return x - y
I can create a class like this
class foo(object):
def __init__(self, func):
self.func = func
abc = foo(func=do1)
abc.func(1, 1) # return 2
abc = foo(func=do2)
abc.func(1, 1) # return 0
Is it possible for me make abc.func to be a method rather than an attribute?
Thanks.
You can add a method to a class like so:
def do(self, x, y):
return x+y
class Foo(object0:
def __init(self):
pass
Foo.bar = do
a = Foo()
a.bar(1,2)
out> 3
Or to an instance:
def do2(x,y):
return x + y
a = Foo()
a.bar2 = do2
a.bar2(3,4)
out> 7
for example
def __str__ (self):
return (x,y)
def main():
how do u print the value of x and y from the def str(self): function
Would really appreciate it thanks!!!
That code doesn't make sense, so I'm extrapolating.
I'm assuming you have some class like so:
class Foobar(object):
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return str((x,y))
def main():
foobar = Foobar(1,2)
main()
In this case, you COULD use string manipulation to handle it.
x_value, y_value = map(str.strip("()"), str(foobar).split(','))
But that's uglier than sin. Why not just reference the values directly?
x_value, y_value = foobar.x, foobar.y
Using the example in Adam's answer:
class Foobar(object):
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return '({foo.x}, {foo.y})'.format(foo=self)
Would result in:
foo = Foobar(2, 3)
print(foo)
'(2, 3)'