Any method to denote object assignment?

2024/10/14 13:26:31

I've been studying magic methods in Python, and have been wondering if there's a way to outline the specific action of:

a = MyClass(*params).method()

versus:

MyClass(*params).method()

In the sense that, perhaps, I may want to return a list that has been split on the '\n' character, versus dumping the raw list into the variable a that keeps the '\n' intact.

Is there a way to ask Python if its next action is about to return a value to a variable, and change action, if that's the case? I was thinking:

class MyClass(object):def __init__(params):self.end = self.method(*params)def __asgn__(self):return self.method(*params).split('\n')def __str__(self):"""this is the fallback if __asgn__ is not called"""return self.method(*params)
Answer

No. You cannot change what happens when you assign to a bare name.

You can change what happens if the assignment target on the left hand side is an attribute or item of an object. You can override a[blah] = ... with __setitem__ and a.blah = ... with __setattr__ (although you can only hook into these on a, not on the object being assigned). But you can't override or in any way influence a = ....

Note that having the right-hand side change based on what is "going to happen" would be even stranger, and very bad. That would mean that

someFunc(MyClass().method())

could be different than

a = MyClass().method()
someFunc(a)

In Python names are just labels attached to objects. Objects don't get to know what labels are attached to them, and that's a good thing. You might assign the result a computation to an intermediate variable just to make subsequent lines more readable, and you don't want that assignment to change the result of that computation.

https://en.xdnf.cn/q/117951.html

Related Q&A

Delete lines found in file with many lines

I have an Excel file (.xls) with many lines (1008), and Im looking for lines that have anything with 2010. For example, there is a line that contains 01/06/2010, so this line would be deleted, leaving …

Combining semaphore and time limiting in python-trio with asks http request

Im trying to use Python in an async manner in order to speed up my requests to a server. The server has a slow response time (often several seconds, but also sometimes faster than a second), but works …

Import of SWIG python module fails with apache

Importing a python mdule throws an exception in django when I run with apache. The same source code works fine with the django development server. I can also import the module from the command line. Th…

Pro-Football-Reference Team Stats XPath

I am using the scrapy shell on this page Pittsburgh Steelers at New England Patriots - September 10th, 2015 to pull individual team stats. For example, I want to pull total yards for the away team (46…

How to delete the last item of a collection in mongodb

I made a program with python and mongodb to do some diaries. Like thisSometimes I want to delete the last sentence, just by typing "delete!" But I dont know how to delete in a samrt way. I do…

Python+kivy+SQLite: How to set label initial value and how to update label text?

everyone,I want to use kivy+Python to display items from a db file. To this purpose I have asked a question before: Python+kivy+SQLite: How to use them together The App in the link contains one screen.…

how to debug ModelMultipleChoiceField [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 7 years ago.Improve…

Standardization/preprocessing for 4-dimensional array

Id like to standardize my data to zero mean and std = 1. The shape of my data is 28783x4x24x7, and it can thought of as 28783 images with 4 channels and dimensions 24x7. The channels need to be standar…

My Python number guessing game

I have been trying to make a number guessing game for Python and so far it has altogether gone quite well. But what keeps bugging me is that it resets the number on every guess so that it is different,…

Class that takes another class as argument, copies behavior

Id like to create a class in Python that takes a single argument in the constructor, another Python class. The instance of the Copy class should have all the attributes and methods of the original clas…