Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove param = self.param and use self.param directly instead #4484

Closed
brosaplanella opened this issue Oct 3, 2024 · 6 comments · Fixed by #4494
Closed

Remove param = self.param and use self.param directly instead #4484

brosaplanella opened this issue Oct 3, 2024 · 6 comments · Fixed by #4494
Labels
difficulty: easy A good issue for someone new. Can be done in a few hours hacktoberfest

Comments

@brosaplanella
Copy link
Member

          if these are the same, then why not just use self.param instead?

Originally posted by @kratman in #4451 (comment)

There are 63 occurrences of param = self.param so I thought this would be better as separate issue.

@brosaplanella
Copy link
Member Author

I think @kratman also had some concerns about the performance of doing param = self.param instead of self.param directly, but please correct me if I am wrong.

@agriyakhetarpal
Copy link
Member

Using param = self.param creates a new reference (in terms of the immutability aspect) and lookup is faster, but we should prefer readability to performance. I would imagine that the performance impact would be more or less minimal and not actually measurable in a reliable manner.

We could switch to using self.param directly since it makes it clear we are working with an instance attribute

@agriyakhetarpal agriyakhetarpal added difficulty: easy A good issue for someone new. Can be done in a few hours hacktoberfest labels Oct 3, 2024
@kratman
Copy link
Contributor

kratman commented Oct 3, 2024

I think @kratman also had some concerns about the performance of doing param = self.param instead of self.param directly, but please correct me if I am wrong.

For clarification what I was saying is that the benefits of a local reference (param) to the member (self.param) are

  • shorter code by dropping the self
  • potentially some time difference between accessing class memory vs function memory (never compared timings for this)

But the costs are

  • hiding that the function is accessing class memory
  • developers potentially needing to look up where the local reference is defined

I think it is better to use the member variable because being clear about which one is used is better for developers. In the background they both point to the same memory location since they are not copies but references. So I would expect time differences to be tiny if they exist at all.

@brosaplanella
Copy link
Member Author

With the comments above, a good compromise could be to keep the param = self.param in the basic models (which are aimed to users who might edit and build on) while for submodels we can switch to self.param as only developers will work on those.

@kratman
Copy link
Contributor

kratman commented Oct 3, 2024

I would say that any memory benefits are minimal compared to the benefits of clarity. So using self.param is in general better. Everyone copies code around, so I don't think there is a benefit of saying that the base class must be one way and the submodels must be another way.

Trying to squeeze out performance by controlling access to member variables is basically the definition of pre-mature optimization. I would be more concerned with people not realizing that param was pointing to self.param in the class. Then they could modify param and not realize they were modifying self.param as well. This is why it is best to be clear by using self.param

@brosaplanella
Copy link
Member Author

Fair point! For clarity, I meant Basic Models (not Base Model) so these are the models where everything is defined in a single script (as opposed to across files, like with the submodels).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
difficulty: easy A good issue for someone new. Can be done in a few hours hacktoberfest
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants