How to add a new field to Julia type?

Mohamed Elsayed

In Python one can use setattr to add a new attribute to an object like the following code

class Foobar:
    def __init__(self):
        pass

foobar = Foobar()
setattr(foobar, 'foo', 123)

print(foobar.foo)

output

123

I know in Julia there is setfield! but it doesn't allow to add a new field like setattr in Python.
so my question is there is a way to add a new field to an object of a composite type?

Isaiah Norton

is there is way to add a new field to an object of a composite type?

No. Julia is intentionally designed without this kind of local dynamic behavior. If you need to dynamically add and remove fields then you're really using the object as a dictionary with different syntax. In Julia, you would use an actual dictionary – Dict or other associative collection – for that kind of usage. This has no worse performance than an object in a language like Python, although it will have worse performance than a statically defined type in Julia, but that is the inherent cost of objects with dynamic fields – which is precisely why objects don't work that way in Julia.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to add the new field to Object in Purescript

分類Dev

How to create new field in type id in android

分類Dev

Is it possible to add a new field to an existing field of RECORD type in bigquery from UI?

分類Dev

How can I add new field to Meteor.users()?

分類Dev

How to add a new Activity type to the Task work item in TFS 2018

分類Dev

MapStruct add a new calculated field to the dto

分類Dev

Elasticsearch: Add new field in big index

分類Dev

Add new field to mongoDB embeded Doc

分類Dev

Firebase - How do I add a new field for each row in an existing database?

分類Dev

Jackson: add suffix according to a field type

分類Dev

How to convert any type into String in Julia

分類Dev

How to add field in MySQL result?

分類Dev

How to get field type in Kendoui?

分類Dev

How to add new line in a file

分類Dev

how to add an new object with lists in it

分類Dev

Bulk add new field to ALL documents in an elasticsearch index

分類Dev

Does ES support to add a new item into the nested field of existing document?

分類Dev

Add new line into Eclipse's Text control field

分類Dev

Not able to add Postion(latitude and longitude) type field in Document dynamically in Vespa

分類Dev

symfony2: trying to add a type="number" attribute to a form field

分類Dev

How to add a field to a JSON object with the jq command?

分類Dev

How to add an GenericRelation field in a form in Django

分類Dev

How to add/remove from a firestore map field?

分類Dev

How to add a field into a snippet in Comfortable Mexican Sofa

分類Dev

How to add a field into a snippet in Comfortable Mexican Sofa

分類Dev

How to type tree with dynamic field names in typescript

分類Dev

How to use the ActiveRecord json field type

分類Dev

How to change the data type of an ADO recordset field?

分類Dev

How implement field with JsonNode type in DTO model

Related 関連記事

  1. 1

    How to add the new field to Object in Purescript

  2. 2

    How to create new field in type id in android

  3. 3

    Is it possible to add a new field to an existing field of RECORD type in bigquery from UI?

  4. 4

    How can I add new field to Meteor.users()?

  5. 5

    How to add a new Activity type to the Task work item in TFS 2018

  6. 6

    MapStruct add a new calculated field to the dto

  7. 7

    Elasticsearch: Add new field in big index

  8. 8

    Add new field to mongoDB embeded Doc

  9. 9

    Firebase - How do I add a new field for each row in an existing database?

  10. 10

    Jackson: add suffix according to a field type

  11. 11

    How to convert any type into String in Julia

  12. 12

    How to add field in MySQL result?

  13. 13

    How to get field type in Kendoui?

  14. 14

    How to add new line in a file

  15. 15

    how to add an new object with lists in it

  16. 16

    Bulk add new field to ALL documents in an elasticsearch index

  17. 17

    Does ES support to add a new item into the nested field of existing document?

  18. 18

    Add new line into Eclipse's Text control field

  19. 19

    Not able to add Postion(latitude and longitude) type field in Document dynamically in Vespa

  20. 20

    symfony2: trying to add a type="number" attribute to a form field

  21. 21

    How to add a field to a JSON object with the jq command?

  22. 22

    How to add an GenericRelation field in a form in Django

  23. 23

    How to add/remove from a firestore map field?

  24. 24

    How to add a field into a snippet in Comfortable Mexican Sofa

  25. 25

    How to add a field into a snippet in Comfortable Mexican Sofa

  26. 26

    How to type tree with dynamic field names in typescript

  27. 27

    How to use the ActiveRecord json field type

  28. 28

    How to change the data type of an ADO recordset field?

  29. 29

    How implement field with JsonNode type in DTO model

ホットタグ

アーカイブ