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

define separate semantics for $, and [[ #255

Merged
merged 9 commits into from
May 13, 2018

Conversation

kevinushey
Copy link
Collaborator

Related: #251

This PR seeks to define different semantics for $ and [[. With this PR:

  • $ is used for attribute access, as Python's . is used;
  • [[ is used for item access, as Python's [[ is used.

For backwards compatibility, with Python (builtin) dictionaries, $ still returns items rather than attributes. We may want to consider changing this in the future.

@kevinushey
Copy link
Collaborator Author

For what it's worth, some of the surprise here might've arisen from how Pandas defines the . operator. For Pandas DataFrames, . will first look at attributes; if no such attribute exists, then it will attempt to look for a column. This implies that code of this form works:

>>> import pandas as pd
>>> pdf = pd.DataFrame({'apple': [1]})
>>> pdf.apple
0    1
Name: apple, dtype: int64

However, if an attribute (method) of the same name exists, then that is chosen instead:

>>> import pandas as pd
>>> pdf = pd.DataFrame({'pop': [1]})
>>> pdf.pop
<bound method DataFrame.pop of    pop
0    1>

@kevinushey kevinushey force-pushed the bugfix/python-access-items-attributes branch from 47f0b76 to 692aff3 Compare May 10, 2018 19:45
@kevinushey
Copy link
Collaborator Author

To help preserve backwards compatibility, I've decided on the following:

  • $ accesses attributes first, items second;
  • [ accesses items first, attributes second.

This should ensure the least amount of breakage in existing code, but it does imply a change in this case for example:

> d <- dict(items = 42)
> d$items
<built-in method items of dict>
> d['items']
42.0

Previously, d$items would have accessed the items item; now it accesses the attribute (bound method) called items.

R/python.R Outdated

# convert
if (convert || py_is_callable(attrib)) {
if (py_has_attr(x, name)) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These checks should only occur if the user is passing in a scalar string (length one character vector).

@jjallaire
Copy link
Member

@kevinushey Is this good to merge?

@kevinushey
Copy link
Collaborator Author

I want to double-check this doesn't regress anything in keras or tensorflow so I'll report back after testing a bit there.

@jjallaire
Copy link
Member

Okay, you can go ahead and merge whenever you think it's ready.

@kevinushey
Copy link
Collaborator Author

Tests pass with both keras and tensorflow with this branch, so I'm going to merge.

@flying-sheep
Copy link
Contributor

I don’t see any warnings. I think it’s quite error-prone to have all operators do everything without any way being the right one.

@t-kalinowski t-kalinowski deleted the bugfix/python-access-items-attributes branch August 25, 2023 15:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants