Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Following calls to static methods with indexing when importing classes

Tags:

oop

matlab

I have a class file myClass.m in a package folder +myPack that's on the path. A simple example of the class file is:

classdef myClass
    properties
        prop
    end

    methods
        function obj = myClass(x)
            obj.prop = x;
        end
    end
end 

Now if I directly call the method and access the property using the full package name, i.e.:

x = myPack.myClass(2).prop;

returns x = 2 correctly. Now, if I try the same by importing this class (and not use the package name):

import myPack.myClass
y = myClass(2).prop

it gives me the following error:

Static method or constructor invocations cannot be indexed. Do not follow the call to the static method or constructor with any additional indexing or dot references.

Why does this work in the first case and not the second? As far as I understood, importing a class mainly allowed one to use the class name without the long package name (among other considerations). What is the difference in these two that causes this error and how can I work around it?

like image 950
abcd Avatar asked May 03 '12 22:05

abcd


People also ask

How do you call a static method from another class?

To call a static method from another class, you use the name of the class followed by the method name, like this: ClassName. methodName().

Can a class method call a static method?

A static method can be called from either a class or object reference. We can call it Utils if foo() is a static function in Class Utils.

What is the difference between @staticmethod and Classmethod?

@staticmethod function is nothing more than a function defined inside a class. It is callable without instantiating the class first. It's definition is immutable via inheritance. @classmethod function also callable without instantiating the class, but its definition follows Sub class, not Parent class, via inheritance.

How do you call a static method from another class in Python?

A static method doesn't have access to the class and instance variables because it does not receive an implicit first argument like self and cls . Therefore it cannot modify the state of the object or class. The class method can be called using ClassName. method_name() as well as by using an object of the class.


1 Answers

Here is some more weird for you: the behavior is different if you are running in the command window, from a script, or from a function!

1) command prompt (1st: ok, 2nd: error)

This is what you've already shown

>> x = myPack.myClass(2).prop
x =
     2

>> import myPack.myClass; y = myClass(2).prop
Static method or constructor invocations cannot be indexed.
Do not follow the call to the static method or constructor with
any additional indexing or dot references. 

2) Script (1st: error, 2nd: error)

testMyClassScript.m

x = myPack.myClass(2).prop
import myPack.myClass; y = myClass(2).prop

and

>> testMyClassScript
Static method or constructor invocations cannot be indexed.
Do not follow the call to the static method or constructor with
any additional indexing or dot references.
Error in testMyClassScript (line 1)
x = myPack.myClass(2).prop 

(the second line would also throw the same error)

3) Function (1st: ok, 2nd: ok)

testMyClassFunction.m

function testMyClassFunction()
    x = myPack.myClass(2).prop
    import myPack.myClass; y = myClass(2).prop
end

and

>> testMyClassFunction
x =
     2
y =
     2

I would definitely call that a bug :) The expected behavior is to give an error in all cases.

like image 137
Amro Avatar answered Oct 05 '22 00:10

Amro