# Module hasproperty, getproperty Inconsistency

**URL:** <https://discourse.julialang.org/t/module-hasproperty-getproperty-inconsistency/79737>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [April 20, 2022, 12:57pm UTC](https://discourse.julialang.org/t/module-hasproperty-getproperty-inconsistency/79737 "2022-04-20T12:57:55Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlexFreik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexfreik/32/35471_2.png) [@AlexFreik](https://discourse.julialang.org/u/AlexFreik)\
**Post date:** [April 20, 2022, 12:57pm UTC](https://discourse.julialang.org/t/module-hasproperty-getproperty-inconsistency/79737/1 "2022-04-20T12:57:55Z")

</div>

When I use `getproperty` of `:sin` in `Main` I get some results:

```julia
julia> getproperty(Main, :sin)
sin (generic function with 13 methods)

```

But when I am trying to use `hasproperty`:

```julia
julia> hasproperty(Main, :sin)
false

```

My questions:

- Is it expected behaviour? It feels a bit counterintuitive
- Is there any other way to check wether function exists in a module before calling `getproperty` and getting exception?

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 20, 2022, 1:22pm UTC](https://discourse.julialang.org/t/module-hasproperty-getproperty-inconsistency/79737/2 "2022-04-20T13:22:21Z")

</div>

> [@AlexFreik](#):
>
> Is there any other way to check wether function exists in a module before calling `getproperty` and getting exception?

The easier part first. Here an example for the different ways to query namespaces and structures

```julia
# For modules

@show isdefined(Main, :sin)
@show isdefined(Main, :tin)

@show getproperty(Main, :sin)

# For structs

struct Example
    d
    Example() = new()
    Example(d) = new(d)
end

e = Example()

@show hasproperty(e, :d)
@show isdefined(e, :d)
# @show getproperty(e, :d) #fails

e = Example(42)

@show hasproperty(e, :d)
@show isdefined(e, :d)
@show getproperty(e, :d) 

```

yielding

```julia
isdefined(Main, :sin) = true
isdefined(Main, :tin) = false
getproperty(Main, :sin) = sin
hasproperty(e, :d) = true
isdefined(e, :d) = false
hasproperty(e, :d) = true
isdefined(e, :d) = true  
getproperty(e, :d) = 42

```

For the second part:

> [@AlexFreik](#):
>
> Is it expected behaviour? It feels a bit counterintuitive

I’ve got somewhat accustomed to it. Do you have a better idea?

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [April 20, 2022, 1:31pm UTC](https://discourse.julialang.org/t/module-hasproperty-getproperty-inconsistency/79737/3 "2022-04-20T13:31:57Z")

</div>

The documentation of `hasproperty` says:

```julia
  hasproperty(x, s::Symbol)

  Return a boolean indicating whether the object x has s as one of its own
  properties.

```

The important bit here is “as one of **its own** properties”.

```julia
julia> hasproperty(Base, :sin)
true

julia> a = 1
1

julia> hasproperty(Main, :a)
true

```

---

<div class="post-metadata">

**Author:** ![AlexFreik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexfreik/32/35471_2.png) [@AlexFreik](https://discourse.julialang.org/u/AlexFreik)\
**Post date:** [April 20, 2022, 1:39pm UTC](https://discourse.julialang.org/t/module-hasproperty-getproperty-inconsistency/79737/4 "2022-04-20T13:39:01Z")

</div>

Yes, `isdefined` seems to be working option in this case, thanks a lot)

> [@goerch](#):
>
> I’ve got somewhat accustomed to it. Do you have a better idea?

Maybe it would be more logical to name it `getdefined` instead of `getproperty`. But it is already too late to discuss it)

---

<div class="post-metadata">

**Author:** ![AlexFreik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexfreik/32/35471_2.png) [@AlexFreik](https://discourse.julialang.org/u/AlexFreik)\
**Post date:** [April 20, 2022, 1:42pm UTC](https://discourse.julialang.org/t/module-hasproperty-getproperty-inconsistency/79737/5 "2022-04-20T13:42:35Z")

</div>

Yes, I see) But it is unfortunate that there is not much documentation on `getproperty(value, name::Symbol)`

```julia
help?> getproperty(value, name::Symbol)
  getproperty(value, name::Symbol)
  getproperty(value, name::Symbol, order::Symbol)

  The syntax a.b calls getproperty(a, :b). The syntax @atomic order a.b calls 
  getproperty(a, :b, :order) and the syntax @atomic a.b calls 
  getproperty(a, :b, :sequentially_consistent).

  Examples
  ≡≡≡≡≡≡≡≡≡≡

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [April 20, 2022, 2:06pm UTC](https://discourse.julialang.org/t/module-hasproperty-getproperty-inconsistency/79737/6 "2022-04-20T14:06:23Z")

</div>

As the documentation says, `getproperty` is literally what is called when you do `a.b`. `module` do `using Base` automatically and therefore has all its exported symbols inside it (and `Main` is not exception to this), so they can be accessed with the dot notation and therefore `getproperty`. If anything, I think what is tripping you is the behavior of `module` not of `getproperty`.

---

<div class="post-metadata">

**Author:** ![AlexFreik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexfreik/32/35471_2.png) [@AlexFreik](https://discourse.julialang.org/u/AlexFreik)\
**Post date:** [April 20, 2022, 2:10pm UTC](https://discourse.julialang.org/t/module-hasproperty-getproperty-inconsistency/79737/7 "2022-04-20T14:10:26Z")

</div>

> [@Henrique\_Becker](#):
>
> If anything, I think what is tripping you is the behavior of `module` not of `getproperty` .

I was mainly confused that you can `get property` of a module that doesn’t `have this property`)

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [April 20, 2022, 2:42pm UTC](https://discourse.julialang.org/t/module-hasproperty-getproperty-inconsistency/79737/8 "2022-04-20T14:42:28Z")

</div>

It has? Every `using` (even the implicit `using Base`) make all exported names properties of the importer/user module. They are just not **their own** properties, and so `hasproperty` ignores them. Maybe `hasproperty` should be renamed `hasownproperty` you mean? Or maybe `hasproperty` should not restrict itself just to owned properties?

---

<div class="post-metadata">

**Author:** ![AlexFreik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexfreik/32/35471_2.png) [@AlexFreik](https://discourse.julialang.org/u/AlexFreik)\
**Post date:** [April 20, 2022, 4:00pm UTC](https://discourse.julialang.org/t/module-hasproperty-getproperty-inconsistency/79737/9 "2022-04-20T16:00:26Z")

</div>

Yes, exactly, something like `hasownproperty` would be less confusing. Because intuitively `getproperty(module, prop)` should return property if and only if `hasproperty(module, prop)` is true

---

<div class="post-metadata">

**Author:** ![devel-chm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/devel-chm/32/3572_2.png) [@devel-chm](https://discourse.julialang.org/u/devel-chm)\
**Post date:** [April 20, 2022, 4:36pm UTC](https://discourse.julialang.org/t/module-hasproperty-getproperty-inconsistency/79737/10 "2022-04-20T16:36:46Z")

</div>

I think the fact that `getproperty(module, prop)` does _not_ conflate  
the `module.prop` or `struct.prop` with `hasproperty(module, prop)` is a good thing.

1. It allows for overload of the `.` syntax
2. You can add props lazily, on-demand, or even self-vivifying
3. Keeping the functionality explicit and composable is a strength of Julia

```julia
julia> struct X end

julia> getproperty(X, :b)
ERROR: type DataType has no field b
Stacktrace:
 [1] getproperty(x::Type, f::Symbol)
   @ Base .\Base.jl:28
 [2] top-level scope
   @ REPL[3]:1

julia> hasproperty(X, :b)
false

```

You can see there is an error if `getproperty(module, prop)` is used with a non-existent prop. Having some alternate return value for this case seems an unnecessary extra step.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [April 20, 2022, 7:11pm UTC](https://discourse.julialang.org/t/module-hasproperty-getproperty-inconsistency/79737/11 "2022-04-20T19:11:40Z")

</div>

I do not see how any of your three points stem from the lack of conflation.

1. How conflating would hinder overload? Asking for consistency does not hinder overload.
2. Again, this is not hindered in any way, so?
3. What does this has to do with the problem at hand? Why it would lack explicitness or composability to, for example, have a separate `hasproperty` and `hasownproperty`?

You example does not call `getproperty(module, prop)` it calls `getproperty(object, prop)` so I am not sure if I understand its purpose.
