![]() |
How to call a class in other class? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to call a class in other class? (/thread-38510.html) |
How to call a class in other class? - 3lnyn0 - Oct-23-2022 Hi! I have a problem in which a farmer want to calculate the area of a land when he buying a new land and to add at the existing one. The shape of land are different, I reduce the shape at 3: circle, square and right angle. I made a class for every shape import math class Circle: def __init__(self,radius): self.radius = radius def area_circle(self): return math.pi*(self.radius**2) class Square: def __init__(self, side): self.side = side def area_square(self): return self.latura**2 class RightAngle: def __init__(self, width, length): self.width = width self.length = length def area_right_angle(self): return self.length * self.widthCan you help me to create the class farmer, I don't know how tie that 3 class with farmer class. And extra if doesn't have a exactly form how should I do it? This part for me know is not very important, more important is the first part. Thanks! RE: How to call a class in other class? - wavic - Oct-23-2022 Probably your farmer class will have a name, age, gender, experience points, and so on data and some methods. You could have a dictionary data field to keep all farmland your farm owns. And when your farmboy or girl buys farmland you create a new object from the class Square. You could pick a name for that parcel because you will have a few of the same shapes and use it as a key in that dict and the object ( you just created ) as a value I don't do much object-oriented programming. One could give you a different way to store that data. But this is a straight approach. Just create the object class Farmer: # class data self.farmland = {} # some class methods def new_farmland(self, name): new_farmland = Square() self.farmland[name] = new_farmlandNow you can change the area methods to be the same name so you can get all the area the farmer owns within a loop. RE: How to call a class in other class? - deanhystad - Oct-23-2022 Your shapes should probably have a common superclass, Shape, that defines the things that shapes can do, like add (+) and __str__. All shapes should have an area attribute, and the area attribute should have the same name for each shape. Getting the area of a circle or a square should look identical from outside the class. You should be able to treat shapes generically without having to know what kind of shape they are. Why do you need a class for the farmer? Are there many farmers you need to keep track of? You might make a Land class that is a collection of shapes, but I see no need for a farmer class. If your shapes are designed well you don't really need a special Land class either. A list should be enough. I envision defining a farmer's land to look like this: land = [Circle(5), Square(7), Rectangle(6, 5)] land.extend([Triangle(11, 7), Shape(33)] print("Sum", sum(land)) print("\n".join(map(str, land))) Now all you have to do is write all the shapes. The base class should do most of the work, and the Circle, Square, etc classes should only do things specific to their shape (calculate area). My Shape base class implements __init__, __str__, __add__ and __radd__. __str__ makes it easy to print the shapes. __add__ lets me do thing like print(Circle(5) + Square(5)). __radd__ lets you do things like sum(list_of_shapes). I decided against making Shape an abstract base class (ABC) so it can be used to represent shapes that are not circles, squares, rectangles or triangles. Depending on your needs you could also implement __eq__ and __gt__ in your base class to support comparing shape areas or sorting lists of shapes by their area. That lets you do things like this: def square_a_shape(shape, step=0.0001): """This is a silly way to square a shape.""" square = Square(0) while square < shape: square.side += step return square circle = Circle(10) square = square_a_shape(circle) print(circle, square)
RE: How to call a class in other class? - GenTorossi - Oct-24-2022 Hi, to call a class in other class there are a few options:
|