# ToString() for CPython 3/PythonNet2

**URL:** <https://forum.dynamobim.com/t/tostring-for-cpython-3-pythonnet2/100259>\
**Category:** Developers\
**Tags:** cpython3\
**Created:** [April 19, 2024, 4:13pm UTC](https://forum.dynamobim.com/t/tostring-for-cpython-3-pythonnet2/100259 "2024-04-19T16:13:27Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alien](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/alien/32/40701_2.png) [@Alien](https://forum.dynamobim.com/u/Alien)\
**Post date:** [April 19, 2024, 4:13pm UTC](https://forum.dynamobim.com/t/tostring-for-cpython-3-pythonnet2/100259/1 "2024-04-19T16:13:27Z")

</div>

This works fine in IronPython but the ToString() breaks in Py3.

How do I write it?

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/9/f/9f3e9f4dea7aa8d40004dbfc173386f637f4f56e.png)

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [April 19, 2024, 4:20pm UTC](https://forum.dynamobim.com/t/tostring-for-cpython-3-pythonnet2/100259/2 "2024-04-19T16:20:13Z")

</div>

The name is already a string. There’s no method for converting a string to a string.

---

<div class="post-metadata">

**Author:** ![Alien](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/alien/32/40701_2.png) [@Alien](https://forum.dynamobim.com/u/Alien)\
**Post date:** [April 19, 2024, 4:22pm UTC](https://forum.dynamobim.com/t/tostring-for-cpython-3-pythonnet2/100259/3 "2024-04-19T16:22:27Z")

</div>

Thanks 🙂  
Strange how it was working in IronPython though!

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [April 19, 2024, 4:25pm UTC](https://forum.dynamobim.com/t/tostring-for-cpython-3-pythonnet2/100259/4 "2024-04-19T16:25:25Z")

</div>

I’d be curious to see how you’re using it in IronPython. `ToString()` is a specific method for converting another object type.

Both versions use `str()` to stringify any object.

---

<div class="post-metadata">

**Author:** ![Alien](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/alien/32/40701_2.png) [@Alien](https://forum.dynamobim.com/u/Alien)\
**Post date:** [April 19, 2024, 4:28pm UTC](https://forum.dynamobim.com/t/tostring-for-cpython-3-pythonnet2/100259/5 "2024-04-19T16:28:57Z")

</div>

Exactly as the screen grab was.  
I’m just checking someone’s got a plan view open before I let the script run (because the script is going to stick stuff onto that level) and giving them a warning if it’s not a plan view.

No inputs to the node.

I’ve deleted the ToString() now though and put it as a CPython3 node.

```auto
import clr
clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager

import System

# Get current Revit from RevitServices
doc = DocumentManager.Instance.CurrentDBDocument
# Get active view
activeView = doc.ActiveView

activeViewType = activeView.GetType()

activeViewTypeName = activeViewType.Name

if activeViewTypeName != "ViewPlan":
	OUT = True, "" , activeViewTypeName
else:
	OUT = False, "Sheets" , activeViewTypeName

```

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [April 19, 2024, 4:53pm UTC](https://forum.dynamobim.com/t/tostring-for-cpython-3-pythonnet2/100259/6 "2024-04-19T16:53:36Z")

</div>

Interesting. I mean it’s not doing anything, so you don’t need it anyway.

It’s important to note that `ToString()` is from the Common Language Runtime (clr) and not an actual python method. The python method is `str()` like I mentioned earlier, so that’s the method you should use regularly unless you know an object specifically uses the `ToString()` method.

---

<div class="post-metadata">

**Author:** ![Alien](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/alien/32/40701_2.png) [@Alien](https://forum.dynamobim.com/u/Alien)\
**Post date:** [April 19, 2024, 4:55pm UTC](https://forum.dynamobim.com/t/tostring-for-cpython-3-pythonnet2/100259/7 "2024-04-19T16:55:02Z")

</div>

For normal Python I always use str() but for some of the weird Revit stuff ToString() seems needed… I’m still not sure exactly when though.

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [April 19, 2024, 5:06pm UTC](https://forum.dynamobim.com/t/tostring-for-cpython-3-pythonnet2/100259/8 "2024-04-19T17:06:35Z")

</div>

Probably because the Revit API is translated through CLR. Just depends on what you’re wanting to convert to a string.

---

<div class="post-metadata">

**Author:** ![jacob.small](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jacob.small/32/161030_2.png) [@jacob.small](https://forum.dynamobim.com/u/jacob.small)\
**Post date:** [April 19, 2024, 5:16pm UTC](https://forum.dynamobim.com/t/tostring-for-cpython-3-pythonnet2/100259/9 "2024-04-19T17:16:08Z")

</div>

Revit API objects are not necessarily Python native. Some of it wraps well, but not all. Those ‘not all’ bits need to be converted to Python native before they can use default Python content. That said it should be VERY rare that objects won’t convert to a string via the Python method `str(obj)`, usually around enums, class instances which are being steered via the clr, and nested content.

---

<div class="post-metadata">

**Author:** ![Alien](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/alien/32/40701_2.png) [@Alien](https://forum.dynamobim.com/u/Alien)\
**Post date:** [April 19, 2024, 5:18pm UTC](https://forum.dynamobim.com/t/tostring-for-cpython-3-pythonnet2/100259/10 "2024-04-19T17:18:37Z")

</div>

Oh dear… I actually can understand what you wrote… I’ve turned into a

 ![th-2381596406](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/c/c/cc7d6bafd06814e9240ebf9a6d8e3c9e10d8dc36.jpeg)

---

<div class="post-metadata">

**Author:** ![GavinNicholls](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/gavinnicholls/32/190442_2.png) [@GavinNicholls](https://forum.dynamobim.com/u/GavinNicholls)\
**Post date:** [April 20, 2024, 4:05am UTC](https://forum.dynamobim.com/t/tostring-for-cpython-3-pythonnet2/100259/11 "2024-04-20T04:05:28Z")

</div>

I’ve been updating my package to CP3 lately and came across this also. For anyone who has the same issue in relation to enums, this is a workaround:

```auto
# Works in IP2.7
# OUT = ViewType.AreaPlan.ToString()

# CP3 workaround
OUT = System.Enum.GetName(ViewType, ViewType.AreaPlan)

```

---

<div class="post-metadata">

**Author:** ![c.poupin](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/c.poupin/32/89152_2.png) [@c.poupin](https://forum.dynamobim.com/u/c.poupin)\
**Post date:** [April 20, 2024, 7:12am UTC](https://forum.dynamobim.com/t/tostring-for-cpython-3-pythonnet2/100259/12 "2024-04-20T07:12:17Z")

</div>

> [@GavinNicholls](#):
>
> For anyone who has the same issue in relation to enums, this is a workaround:

for info, It’s solved in PythonNet3

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/a/c/acc5c0ac20d96ff65a011c45722abb51c6e9bd8a.jpeg)

with PythonNet3 Net objects are no longer automatically converted into Python objects as with PythonNet2
