My questions are the code I am running keeps giving me points has no argument, below is my code, please help check
class Point:
def _init_(self, x, y):
self.x = x
self.y = y
def draw(self):
print(f"Point ({self.x}, {self.y})")
point = Point(1, 2)
point.draw(point)
Related
am trying to call the class method which is used again in the main class
from src.another_class import B
class A:
def __init__(self, x):
self.x = x
def one():
a = 'doing something'
if a :
add, sub = B.do_something(x, y, z)
def add(a, b):
pass
def sub(a, b):
pass
#this will be in another folder called source
from A_class import A
class B:
def __init__(self, s):
self.s = s
def do_something(x, y, z):
add = A.two(x, y)
sub = A.sub(x, z)
return add, sub
is there a best way to do this. am new to python oops concepts.
The code is giving me an error. It's supposed to add points on a Cartesian plane.
class Cluster(object):
def __init__(self, x, y):
self.center = Point(x, y)
self.points = []
def update(self):
sum_ = sum(self.points, Point(0, 0))
len_ = float(len(self.points))
self.center = Point(sum_.x/len_, sum_.y/len_)
self.points = []
def add_point(self, point):
self.points.append(point)
sum_ = sum(self.points, Point(0, 0))
The sum() function only only works for types which support addition. A class can implement addition using the __add__ function. If this is indeed the problem you are facing, define in your Point class a function like:
class Point:
# ...
def __add__(self, other):
return Point(self.x + other.x, self.y + other.y)
I want to create a class(say, LockedAttributes) to access(READ/WRITE) some attributes safely by multiple threads.I want to pass those attributes that I want to share as a list to the LockedAttributes class. Some of the list elements are itself class objects with it's own setter and getter.
How can i access those setter/getter from a LockedAttribute class obj?
My use of getattr() setattr() might be wrong.
sample code:
class Coord:
def __init__(self, x=0.0, y=0.0, z=0.0):
self.x = x
self.y = y
self.z = z
def set_coordinator(self, x, y, z):
self.x = x
self.y = y
self.z = z
def get_coordinator(self):
return self.x, self.y, self.z
class LockedAttributes(object):
def __init__(self, obj):
self.__obj = obj
self.__lock = RLock()
def getmyPosition(self):
with self.__lock:
return self.__obj[0]
def getmySpeed(self):
with self.__lock:
return self.__obj[1]
def getcolPosition(self):
with self.__lock:
return self.__obj[2]
def getDistfromCol(self):
with self.__lock:
getattr(self, self.__obj[3])
def setDistfromCol(self, value):
with self.__lock:
setattr(self, self.__obj[3], value)
def getcolactivationFlag(self):
with self.__lock:
getattr(self, self.__obj[4])
def setcolactivationFlag(self, value):
with self.__lock:
setattr(self, self.__obj[3], value)
class OBU():
def __init__(self):
pos = Coord()
speed = Coord()
colpos = Coord()
distance_from_accident = 0.0
Flag = False
self.shared_attributes = LockedAttributes([ pos, speed, colpos, distance_from_accident, Flag])
mypos= self.shared_attributes.getmyPosition()
mypos.get_coordinator() # Not workinh
The __init__ method of the LockedAttributes class should take an argument so that you can actually pass a list object to it.
Change:
class LockedAttributes(object):
def __init__(self):
self.__obj = object
self.__lock = RLock()
To:
class LockedAttributes(object):
def __init__(self, obj):
self.__obj = obj
self.__lock = RLock()
I have a point class which has 2-d point instances. I also have a magnitude function inside which returns the magnitude of said points. Below is my code...
class Point:
# """2-D Point objects."""
def __init__(self, x, y):
# """Initialize the Point instance"""
self.x = x
self.y = y
def get_magnitude(self):
# """Return the magnitude of vector from (0,0) to self."""
return math.sqrt(self.x ** 2 + self.y ** 2)
def __str__(self):
return 'Point at ({}, {})'.format(self.x,self.y)
def __repr__(self):
return "Point(x={},y={})".format(self.x,self.y)
point = Point(x=3, y=4)
print(str(point))
print(repr(point))
print(point)
...After doing all of this, the final part of this is to implement a default point of (0,0). Any suggestions on how to do this? It should work like this...
point2 = Point()
print(point2)
Point(x=0, y=0)
point3 = Point(y=9)
print(point3)
Point(x=0, y=9)
You can pass in default arguments to an initializer, just like you would any other function.
def __init__(self, x=0, y=0):
# """Initialize the Point instance"""
self.x = x
self.y = y
I have the below code which is working.
class Point:
# """2-D Point objects."""
def __init__(self, x=0, y=0):
# """Initialize the Point instance"""
self.x = x
self.y = y
def get_magnitude(self):
# """Return the magnitude of vector from (0,0) to self."""
return math.sqrt(self.x ** 2 + self.y ** 2)
def __str__(self):
return 'Point at ({}, {})'.format(self.x,self.y)
def __repr__(self):
return "Point(x={},y={})".format(self.x,self.y)
point = Point(x=3, y=4)
print(str(point))
print(repr(point))
print(point)
point2 = Point()
print(point2)
point3 = Point(y=9)
print(point3)
I want to change the get_magnitude method into a property method named magnitude which works as shown below.
point = Point(3, 4)
point
Point(x=3, y=4)
point.magnitude
5.0
point3 = Point(y=9)
point3.magnitude
9.0
How would I do this?
import math
class Point:
"""2-D Point objects."""
def __init__(self, x=0, y=0):
"""Initialize the Point instance"""
self.x = x
self.y = y
#property
def magnitude(self):
"""Return the magnitude of vector from (0,0) to self."""
return math.sqrt(self.x ** 2 + self.y ** 2)
def __str__(self):
return 'Point at ({}, {})'.format(self.x,self.y)
def __repr__(self):
return "Point(x={},y={})".format(self.x,self.y)
point = Point(3, 4)
print(point)
print(point.magnitude)
point3 = Point(y=9)
print(point3.magnitude)
Prints:
Point at (3, 4)
5.0
9.0