andyname
andyname

Reputation: 233

Is there something like python __dict__ in Julia?

Python has dict, doc, init and so on. Is there something like that in Julia? How can I know names of functions of Julia packages?

enter image description here

Upvotes: 2

Views: 224

Answers (2)

Vitaliy Yakovchuk
Vitaliy Yakovchuk

Reputation: 493

In addition to the function names (e.g. names(Gadfly)), which is used for modules. If you want to get all the attributes of the object, there are two functions for that:

  1. fieldnames - returns a list of the fields for the object e.g.
struct Point
  x
  y
end
> propertynames(Point(2,3))

(:x, :y)

propertynames - returns a list of all the properties for the object. Usually the same as fieldnames plus user-defined properties (in most cases, you should use this function instead of fieldnames)

Upvotes: 6

Bogumił Kamiński
Bogumił Kamiński

Reputation: 69869

Use the names function to get a list of all names exported by a module (as this is what I assume you are looking for). Note that the list will in particular include: functions, types, variables, and other modules. Here is an excerpt from its docstring giving you more details:

names(x::Module; all::Bool = false, imported::Bool = false)

Get an array of the names exported by a Module, excluding deprecated names. If all is true, then the list also includes non-exported names defined in the module, deprecated names, and compiler-generated names. If imported is true, then names explicitly imported from other modules are also included.

Because of the Julia design you should be aware of two issues:

  • some packages opt not to export names, but assume that they should be always qualified; this is the case of e.g. CSV.jl package
  • objects other than functions can be made callable in Julia (types are callable as constructors, variables can be turned into functors)

Upvotes: 5

Related Questions