What Does X F() Mean

Is there a difference between x.f.call(x, ...) and x.f(...)?

There is no difference between the two.

If we check the language specification for normal function calls and then Function.prototype.call we can see that they behave exactly the same.

Namely,

x.f.call(x) does this:

Return the result of calling the [[Call]] internal method of func, providing thisArg as the this value and argList as the list of arguments.

Where a normal call does:

Return the result of calling the [[Call]] internal method on func, providing thisValue as the this value and providing the list argList as the argument values.

Where this has resolution (specified in 11.2.3 7.b.i in the spec).

All modern JavaScript implementations (yes, even IE8 modern) respect this.

How to refer to position when using xf:setvalue function with iterate

The following works:

<xf:action iterate="instance('fr-send-submission-params')/@*">
<xf:var name="p" value="position()"/>
<xf:setvalue ref="." value="$p"/>
</xf:action>

I don't think you can get away just with xf:setvalue, because ref changes the evaluation context of the expression to a single item which means that position() returns 1 within value.

A warning as I see that you iterate on attributes: I don't think that attribute position is guaranteed to be consistent.

Update:

The following works if you have elements, but then you need to have knowledge of the items iterated within the xf:setvalue:

<xf:setvalue
event="DOMActivate"
iterate="value"
ref="."
value="count(preceding-sibling::value) + 1"/>

So I think that the option with an enclosing action is much clearer.

Two methods inherited from one method in class are different in instances, aren't they?

When you access a function through an instance that is defined on the class, a bound-method object is created each time. From the docs:

What exactly happens when a method is called? You may have noticed
that x.f() was called without an argument above, even though the
function definition for f() specified an argument. What happened to
the argument? Surely Python raises an exception when a function that
requires an argument is called without any — even if the argument
isn’t actually used…

Actually, you may have guessed the answer: the special thing about
methods is that the instance object is passed as the first argument of
the function. In our example, the call x.f() is exactly equivalent to
MyClass.f(x). In general, calling a method with a list of n arguments
is equivalent to calling the corresponding function with an argument
list that is created by inserting the method’s instance object before
the first argument.

If you still don’t understand how methods work, a look at the
implementation can perhaps clarify matters. When an instance attribute
is referenced that isn’t a data attribute, its class is searched. If
the name denotes a valid class attribute that is a function object, a
method object is created by packing (pointers to) the instance object
and the function object just found together in an abstract object:
this is the method object.

Note, this happens every time you access a method:

>>> class Foo:
... def bar(self): pass
...
>>> f = Foo()
>>> f.bar is f.bar
False

How does this work? Well, actually, functions are descriptor objects, note the presence of a __get__:

>>> def func(): pass
...
>>> func.__get__
<method-wrapper '__get__' of function object at 0x101e38ae8>

In other words, you can think of Python functions as being implemented thusly:

class Function(object):
. . .
def __get__(self, obj, objtype=None):
"Simulate func_descr_get() in Objects/funcobject.c"
if obj is None:
return self
return types.MethodType(self, obj)

Of course, they aren't implemented in Python (in CPython at least!).

Also note, accessing the function directly on the class, of course, doesn't do this (at least on Python 3, which you've tagged on your question):

>>> Foo.bar is Foo.bar
True

calling a function declared x.f = function() from other files -- js

If you are doing it in the order specified then you are overwriting your x variable with a new object.

x.funstuff = function() {};

means that x = { funstuff: function() {} };

then x = {}; replaces it

If you are trying to call the function do it as:

x.funstuff();

simple instantiations of python classes

When inside the REPL (or the Python console, or whatever) the value returned by the last statement will always be printed. If it is just a value the value will be printed:

>>> 1
1

If it is an assignment, then nothing will be printed:

>>> a = 1

But, watch this:

>>> a = 1
>>> a
1

Ok, so in your code above:

>>> x=MyClass()
>>> x # I'm adding this :-). The number below may be different, it refers to a
# position in memory which is occupied by the variable x
<__main__.MyClass instance at 0x060100F8>

So, the value of x is an instance of MyClass located at a spot in memory.

>>> x.i
12345

The value of x.i is 12345, so it will be printed as above.

>>> x.f
<bound method MyClass.f of <__main__.MyClass instance at 0x060100F8>>

The value of f is a method of x (that's what it means to have def in front of something, it is a method). Now, since it is a method, let's call it by adding the () after it:

>>> x.f()
'hello world'

The value returned by f method on the MyClass instance in the variable x is 'hello world'! But wait! There are quotes. Let's get rid of them by using the print function:

>>> print(x.f()) # this may be print x.f() (note the number of parens)
# based on different versions of Python.
hello world

Basic questions about Java syntax

1.) why is something like x = new C(); not required here?

x is only an uninitialized reference. If you want to use it, you indeed need to assign something to it.

2.) Is "this" in this.bump necessary?

No. this refers to the current object, and since x is a member of the current object you do not need it in this case.

2a) Likewise, this.bump(this.y,x) would be equivalent right?

No. y is a local variable, you can not access it with this.y.

Difference between methods and functions, in Python compared to C++

Needs Attention: This answer seems to be outdated. Check this

A function is a callable object in Python, i.e. can be called using the call operator (though other objects can emulate a function by implementing __call__). For example:

>>> def a(): pass
>>> a
<function a at 0x107063aa0>
>>> type(a)
<type 'function'>

A method is a special class of function, one that can be bound or unbound.

>>> class A:
... def a(self): pass
>>> A.a
<unbound method A.a>
>>> type(A.a)
<type 'instancemethod'>

>>> A().a
<bound method A.a of <__main__.A instance at 0x107070d88>>
>>> type(A().a)
<type 'instancemethod'>

Of course, an unbound method cannot be called (at least not directly without passing an instance as an argument):

>>> A.a()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unbound method a() must be called with A instance as first argument (got nothing instead)

In Python, in most cases, you won't notice the difference between a bound method, a function, or a callable object (i.e. an object that implements __call__), or a class constructor. They all look the same, they just have different naming conventions. Under the hood, the objects may look vastly different though.

This means that a bound method can be used as a function, this is one of the many small things that makes Python so powerful

>>> b = A().a
>>> b()

It also means that even though there is a fundamental difference between len(...) and str(...) (the latter is a type constructor), you won't notice the difference until you dig a little deeper:

>>> len
<built-in function len>
>>> str
<type 'str'>

What is a method in Python?

It's a function which is a member of a class:

class C:
def my_method(self):
print("I am a C")

c = C()
c.my_method() # Prints("I am a C")

Simple as that!

(There are also some alternative kinds of method, allowing you to control the relationship between the class and the function. But I'm guessing from your question that you're not asking about that, but rather just the basics.)



Related Topics



Leave a reply



Submit