Skip to content

foreach over dynamic PyObject #1680

Closed
Closed

Description

@CodingMadness

Environment

  • Pythonnet version: 3.0.0
  • Python version: 3.9.5
  • Operating System: windows 10
  • .NET Runtime: .NET 6

Details

  • Describe what you were trying to get done.

  • foreach a tuple at runtime via "dynamic" keyword

  • What commands did you run to trigger this issue? If you can provide a
    [Minimal, Complete, and Verifiable example] (http://stackoverflow.com/help/mcve)
    this will help us understand the issue.
    `
    dynamic sys = Py.Import("sys");
    dynamic path2 = sys.path;

    foreach (PyObject item in path2)
    {
    Console.WriteLine(item);
    }
    `

  • If there was a crash, please include the traceback here.

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: "Cannot implicitly convert type 'Python.Runtime.PyObject' to 'System.Collections.IEnumerable'. An explicit conversion exists (are you missing a cast?)"

Activity

  1. lostmsu commented on Jan 25, 2022

    @lostmsu
    Member

    As a workaround you can do item in new PyTuple(path2), or item in PyIter.GetIter(path2).

  2. changed the title [-]Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: "Cannot implicitly convert type 'Python.Runtime.PyObject' to 'System.Collections.IEnumerable'. An explicit conversion exists (are you missing a cast?)"[/-] [+]`foreach` over dynamic `PyObject`[/+] on Jan 25, 2022
  3. CodingMadness commented on Jan 25, 2022

    @CodingMadness
    Author

    @lostmsu
    thx for the quick response :)

    I have to add tho, that the 1.) approach works good! (item in new PyTuple(path2))
    The 2.) approach fails aswell, with the same exception as in this thread! (PyIter.GetIter(path2))

  4. added this to the 3.0.0 milestone on Feb 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions