29 August 2025
Dmytrotext not yet in the index
I know they must be defined, I'm saying how can I pass an existing object as argument to a class's init without rewriting again and again.
for example we have a class named Fruits that its init has 10 parameters, you can create objects with it like apple, banana, pineapple and whatever. Now whenever I want to use that class in a child's class, I literally have to redefine all those parameters again.
so I thought maybe there is a way, like making an object, then passing it to another object as argument, so next time I would create an object, I don't need to redefine all the parameters.
for example turning this:
z = Apple("Apple, "red", "Fuji", "X_place")
into the --->
z = Apple(x, "Fuji", "X_place")
ArtanI know they must be defined, I'm saying how can I pass an existing object as argument to a class's init without rewriting again and again.
for example we have a class named Fruits that its init has 10 parameters, you can create objects with it like apple, banana, pineapple and whatever. Now wheneve
Why would you (want to) create an instance of Fruit in the first place?
Artan😂🤦♂️ why is it so hard to explain
My guess is because you don't know OOP very well and are trying to use it in a way that it isn't intended for. It's like you're trying to explain a problem that maybe isn't one at all, just because you think that what you're trying is the solution. Instead, try to explain what your intention is. Because, as I see it, there's no point in creating an instance of Fruit in the first place.
x = Apple("Apple, "red", "Fuji", "X_place")
y = Apple("Apple, "red", "Fuji", "Y_place")
z = Apple("Apple, "red", "Fuji", "Z_place")
n = Apple("Apple, "red", "new", "N_place")
m = Apple("Apple, "red", "new", "M_place")
You see, whenever I want to create an object of child class (Apple), I have to rewrite the first two arguments?
Is it possible to somehow shorten it to something like this?
Test = Fruit("Apple", "red")
x = Apple(Test, "Fuji", "X_place")
y = Apple(Test, "Fuji", "Y_place")
z = Apple(Test, "Fuji", "Z_place")
Artanx = Apple("Apple, "red", "Fuji", "X_place")
y = Apple("Apple, "red", "Fuji", "Y_place")
z = Apple("Apple, "red", "Fuji", "Z_place")
n = Apple("Apple, "red", "new", "N_place")
m = Apple("Apple, "red", "new", "M_place")
You see, whenever I want to create an object of child class (Apple), I have to re
Instead of letting Apple handle both cases, you could make a factory function that “remembers” the shared values:
def apple_factory(kind, color):
def create(variety, place):
return Apple(kind, color, variety, place)
return create
make_red_apple = apple_factory("Apple", "red")
x = make_red_apple("Fuji", "X_place")
y = make_red_apple("Fuji", "Y_place")
z = make_red_apple("Fuji", "Z_place")
That keeps Apple’s constructor simple, but you still avoid rewriting the first two args.
Artanx = Apple("Apple, "red", "Fuji", "X_place")
y = Apple("Apple, "red", "Fuji", "Y_place")
z = Apple("Apple, "red", "Fuji", "Z_place")
n = Apple("Apple, "red", "new", "N_place")
m = Apple("Apple, "red", "new", "M_place")
You see, whenever I want to create an object of child class (Apple), I have to re
Factory (as answered before me) not an unusual way to approach this. But your use case that you have presented is an unusual case. What I mean by that is that you would usually not create instances of class in your code that has so many hard-coded values. And if every Apple needs a value that is "Apple", the question arises if it shouldn't be in the class instead of requesting the value on instantiation of the class. Which means that in reality you would probably have a list of objects and possibly a data structure to iterate for the creation of those objects that end up being in a list. Completely eliminating that use case.
Artanx = Apple("Apple, "red", "Fuji", "X_place")
y = Apple("Apple, "red", "Fuji", "Y_place")
z = Apple("Apple, "red", "Fuji", "Z_place")
n = Apple("Apple, "red", "new", "N_place")
m = Apple("Apple, "red", "new", "M_place")
You see, whenever I want to create an object of child class (Apple), I have to re
Additionally, there's this
class Fruit:
def __init__(self, name, color):
self.name = name
self.color = color
class Apple(Fruit):
def __init__(self, variety, region):
super().__init__("Apple", "Red")
self.variety = variety
self.region = region
Everything depends on the use case.
id 8327492889I have 0 knowledge of coding any one give me tips about coding.
You don't need tips then, you need something you can learn it from.
AnvarCan you guys give me some advice? I'm just starting to learn programming; where do I begin?
Get a book or pick a course and start writing Python code.
Real Python.com and the Python docs are a good starting point. Once you are familiar with the basics take on some coding challenges or small projects.
30 August 2025
Tele Tacos@Doragonsureiya_bot roadmap to learn Unix
This is a python programming group and the bot can't answer you like that
Artantext not yet in the index
u can definite magic method called iter on Fruit, it returns iter((self.name, self.color)). Unpack it to Apple.
class Fruit:
def __init__(self, name, color):
self.name = name
self.color = color
def __iter__(self):
return iter((self.name, self.color))
class Apple(Fruit):
def __init__(self, name, color, variety, region):
super().__init__(name, color)
self.variety = variety
self.region = region
def describe():
print(f"{self.name, self.color, self.variety, self.region} are correct.")
x = Fruit("Apple", "red")
y = Apple("Apple", "red", "Fuji", "X_place")
z = Apple(*x, "Fuji", "X_place")