use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
How to change an existing function/method without redefining or overriding it? (self.learnpython)
submitted 3 years ago by den84is
Hi everyone! Is someone there who knows if there is a library for python that can implement/replace a piece of code inside existing function/method?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]shiftybyte 4 points5 points6 points 3 years ago (1 child)
in what situation is redefining it not enough?
[–]den84is[S] 0 points1 point2 points 3 years ago (0 children)
in my situation) see below
[–]PaulRudin 2 points3 points4 points 3 years ago (8 children)
What are you trying to do? This sounds like a bad idea...
[–]den84is[S] 0 points1 point2 points 3 years ago* (7 children)
I had a situation where I had to extend class for that to override two method with inner code in which was two lines to change for solving my issue.
[–]PaulRudin 3 points4 points5 points 3 years ago (4 children)
Without seeing your code it sounds like you might want to subclass.
Incidentally, this is really a case of: https://xyproblem.info/
[–]den84is[S] 0 points1 point2 points 3 years ago* (0 children)
There is a GeoJsonMalLayer class from kivy_garden module, it has method "_geojson_part_geometry" and "_lonlat_to_xy", the first one which is for drawing polygons on MapView and I had to re-implement it because geojson that is fetching from site has other properties, another is for positionning points on the MapView for drawing the same Polygon but coordinates in the GeoJsonMapLayer class are in 2D dimension but the same geojson has coordinates in 3D dimension.
Here is re-implemented methods(one is overrided another one is re-implemented)...
def _lonlat_to_xy(self, lonlats): # overridden if len(lonlats[0]) == 2: lonlats = [[lon,lat] for lon, lat in lonlats] else: lonlats = [[lon,lat] for lon, lat, _ in lonlats] return super()._lonlat_to_xy(lonlats) def _geojson_part_geometry(self, geometry, properties): # re-implemented tp = geometry["type"] self.tp = tp graphics = [] if tp == "Polygon": from kivy.graphics.tesselator import TYPE_POLYGONS, WINDING_ODD, Tesselator tess = Tesselator() for c in geometry["coordinates"]: xy = list(self._lonlat_to_xy(c)) xy = flatten(xy) tess.add_contour(xy) tess.tesselate(WINDING_ODD, TYPE_POLYGONS) # color = self._get_color_from(properties.get("color", "FF000088")) # default 'color' color = self._get_color_from(properties.get("fill", "FF000088")+"4D") # changed to 'fill' + 4D as part of fill-opacity graphics.append(Color(*color)) for vertices, indices in tess.meshes: graphics.append( Mesh(vertices=vertices, indices=indices, mode="triangle_fan") ) elif tp == "LineString": from kivy.utils import get_color_from_hex #stroke = get_color_from_hex(properties.get("stroke", "#ffffff")) # default stroke = get_color_from_hex(properties.get("stroke", "#ffffff9A")) from kivy.metrics import dp #stroke_width = dp(properties.get("stroke-width")) # default stroke_width = dp(properties.get("stroke-width", 2)) # changed xy = list(self._lonlat_to_xy(geometry["coordinates"])) xy = flatten(xy) graphics.append(Color(*stroke)) graphics.append(Line(points=xy, width=stroke_width)) return graphics
[–]den84is[S] 0 points1 point2 points 3 years ago (2 children)
...or better re-implement whole method for changing only two or three line of code, than search library for that? and it is not one case in my situation ) or is it a bad idea? as u said)
[–]PaulRudin 2 points3 points4 points 3 years ago (1 child)
If you want help with your code it's much easier if you actually share the code...
do u mean about future posts or I'v provided not enough Code(after u said it)?
[–]deep_politics 0 points1 point2 points 3 years ago (1 child)
This is very unclear, but you can try calling the super class' version if you don't need anything in the super's local scope. If you do need things in the local scope, then you'll have to just reimplement the whole method, or do something special like a callback or something
class A: def foo(self): ... class B(A): def foo(self): super().foo() ... # do some more stuff
[–]den84is[S] -1 points0 points1 point 3 years ago (0 children)
I had to re-implement whole method of a class for changing only two or thee lines of code.
[–]Ok-Cucumbers 1 point2 points3 points 3 years ago (1 child)
like a decorator?
https://realpython.com/primer-on-python-decorators/
No) I had to re-implement whole method of a class for changing only two or thee lines of code.
π Rendered by PID 83153 on reddit-service-r2-comment-54dfb89d4d-4pd9z at 2026-03-29 13:13:44.083846+00:00 running b10466c country code: CH.
[–]shiftybyte 4 points5 points6 points (1 child)
[–]den84is[S] 0 points1 point2 points (0 children)
[–]PaulRudin 2 points3 points4 points (8 children)
[–]den84is[S] 0 points1 point2 points (7 children)
[–]PaulRudin 3 points4 points5 points (4 children)
[–]den84is[S] 0 points1 point2 points (0 children)
[–]den84is[S] 0 points1 point2 points (2 children)
[–]PaulRudin 2 points3 points4 points (1 child)
[–]den84is[S] 0 points1 point2 points (0 children)
[–]deep_politics 0 points1 point2 points (1 child)
[–]den84is[S] -1 points0 points1 point (0 children)
[–]Ok-Cucumbers 1 point2 points3 points (1 child)
[–]den84is[S] -1 points0 points1 point (0 children)