# Asset class keyword causing error in Python

**URL:** <https://forum.alpaca.markets/t/asset-class-keyword-causing-error-in-python/2865>\
**Category:** App-Integration\
**Created:** [October 2, 2020, 10:52pm UTC](https://forum.alpaca.markets/t/asset-class-keyword-causing-error-in-python/2865 "2020-10-02T22:52:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pkdenver](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/pkdenver/32/1363_2.png) [@pkdenver](https://forum.alpaca.markets/u/pkdenver)\
**Post date:** [October 2, 2020, 10:52pm UTC](https://forum.alpaca.markets/t/asset-class-keyword-causing-error-in-python/2865/1 "2020-10-02T22:52:18Z")

</div>

Can the Asset element “class” be renamed to something different? It’s a python keyword and problematic in my code. It gives me an error when I try to access it. Code snippet and error indicated below:

```
asset = api.get_asset('AAPL')
asset_class = asset.class
                    ^

```

SyntaxError: invalid syntax

Every other element can be accessed with no problem (e.g. asset\_name = asset.name).

Thanks

---

<div class="post-metadata">

**Author:** ![pkdenver](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/pkdenver/32/1363_2.png) [@pkdenver](https://forum.alpaca.markets/u/pkdenver)\
**Post date:** [October 2, 2020, 11:31pm UTC](https://forum.alpaca.markets/t/asset-class-keyword-causing-error-in-python/2865/2 "2020-10-02T23:31:51Z")

</div>

A suggestion… Maybe add a new class attribute of a different name, with the duplicate data from “class”. e.g. asset\_class

---

<div class="post-metadata">

**Author:** ![pkdenver](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/pkdenver/32/1363_2.png) [@pkdenver](https://forum.alpaca.markets/u/pkdenver)\
**Post date:** [October 3, 2020, 12:32am UTC](https://forum.alpaca.markets/t/asset-class-keyword-causing-error-in-python/2865/3 "2020-10-03T00:32:41Z")

</div>

I wrote the following function to convert Asset instance into a dict:

def assettodict (asset):

```
# Asset is a nonserializable object that contains an innaccessible
# attribute (class). The problem is that "class" is a python
# keyword, and cannot be accessed directly (asset.class).
# 
# Since it's not iterable or serializable, it cannot be directly
# type converted into a python dict, nor can it be converted
# into json directly. So, we have to jump through a few gyrations -
# hence utilizing jsonpickle as an interim step.

jsonpicklestring = jsonpickle.encode (asset)
assetpickle = json.loads(jsonpicklestring)
assetdict = assetpickle["_raw"]

return (assetdict)
```

---

<div class="post-metadata">

**Author:** ![dan\_whitnable](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/dan_whitnable/32/1323_2.png) [@dan\_whitnable](https://forum.alpaca.markets/u/dan_whitnable)\
**Post date:** [October 3, 2020, 3:35am UTC](https://forum.alpaca.markets/t/asset-class-keyword-causing-error-in-python/2865/4 "2020-10-03T03:35:52Z")

</div>

Using ‘class’ for the type of an asset is a bit unfortunate. It conflicts with the python reserved word ‘class’. I get around it by using the python `getattr` function. Like this

```
asset = api.get_asset('AAPL')
asset_class = getattr(asset, 'class')

```

As mentioned above by pkdenver, an asset (or any object) could also be turned into a python dict. That way the attributes become keys and can be fetched using bracket notation. There’s the built in python `vars` function just for the purpose of turning objects into dicts. Like this.

```
asset = api.get_asset('AAPL')
asset_dict = vars(asset)['_raw']
asset_class = asset_dict['class']

```

But yes, it would have been better to name the attribute something else and not need to do these workarounds.

---

<div class="post-metadata">

**Author:** ![Brian\_Rowe](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/brian_rowe/32/2322_2.png) [@Brian\_Rowe](https://forum.alpaca.markets/u/Brian_Rowe)\
**Post date:** [June 15, 2021, 4:43pm UTC](https://forum.alpaca.markets/t/asset-class-keyword-causing-error-in-python/2865/5 "2021-06-15T16:43:46Z")

</div>

> [@dan\_whitnable](#):
>
> `asset_class = getattr(asset, 'class')`

What assets classes are even possible? There is no list in the documentation so why do you even what this?

---

<div class="post-metadata">

**Author:** ![digitechroshni](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/digitechroshni/32/5888_2.png) [@digitechroshni](https://forum.alpaca.markets/u/digitechroshni)\
**Post date:** [March 21, 2024, 12:52pm UTC](https://forum.alpaca.markets/t/asset-class-keyword-causing-error-in-python/2865/6 "2024-03-21T12:52:11Z")

</div>

> [@pkdenver](#):
>
> Can the Asset element “class” be renamed to something different? It’s a python keyword and problematic in my code. It gives me an error when I try to access it. Code snippet and error indicated below:
> 
> ```auto
> asset = api.get_asset('AAPL')
> asset_class = asset.class
> ^
> 
> ```
> 
> SyntaxError: invalid syntax
> 
> Every other element can be accessed with no problem (e.g. asset\_name = asset.name).
> 
> Thanks

Yes, you can rename the `class` attribute to something else to avoid conflicts with the Python keyword. You can use the `getattr()` function to access attributes dynamically. Here’s how you can do it:

asset = api.get\_asset(‘AAPL’)  
asset\_class = getattr(asset, ‘class’)
