ChatCrawlersearch across public Telegram Open the app
P

Python

snapshot for August 2025
August 2025 ×
29 August 2025
A
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")
A
😂🤦‍♂️ why is it so hard to explain
C
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.
A
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")
I
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.
apple_data = [ ("Fuji", "X_place"), ("Fuji", "Y_place"), ("Fuji", "Z_place"), ("new", "N_place"), ("new", "M_place"), ] apples = [] for variety, region in apple_data: apples.append(Apple("Apple", "Red", variety, region))
C
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.
A
Can you guys give me some advice? I'm just starting to learn programming; where do I begin?
Look, either you get a book and you can stop worrying about where to begin and what to do next, or you find a roadmap and try to find resources for every topic on your own.
C
A learning Path is something between a course and a book (some contain chapters from books). But I really like to recommend books. There's somebody that has thought about how to teach Python from the beginning to the end (up to a certain point, there's no end).
30 August 2025
I
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")
Open in Telegram Каталог площадок Искать в ChatCrawler

A snapshot of an open public feed from the search index ChatCrawler — “Google for public Telegram”; refreshed as the venue is crawled. Times are UTC.

Public content only, official Telegram API. About · FAQ · What we do not do · Remove a page · Catalog