I am trying to write a script that updates a feature layer on arcgis online. I need to replace the data source to the new feature class each time I run the script. How can I accomplish this?
Assuming you're talking about hosted feature layers (as opposed to those coming from Enterprise), I could be mistaken but I don't believe this is possible. Feature classes are completely abstracted away in AGOL, and users (along with the API) only have access to feature layers.
I'm going to take a guess and speculate you want to do this to make web maps, apps, forms, etc. use an updated data source, rather than modifying the maps/apps/etc. to use to a new feature layer? I believe it's possible to change the source of a feature layer view (although I'm not sure how to do it in Python), so a solution may be to do this:
Why are you needing to replace the data source? Can you not truncate and append the data in your hosted feature layer to accomplish what you are needing?
Also, AGOL now has the ability to swap views so if you want to do something like that it could be helpful.