“TypeError: method() takes 1 positional argument but 2 were given” but I only passed one

In Python, this: my_object.method(“foo”) … is syntactic sugar, which the interpreter translates behind the scenes into: MyClass.method(my_object, “foo”) … which, as you can see, does indeed have two arguments – it’s just that the first one is implicit, from the point of view of the caller. This is because most methods do some work with … Read more

Is there a generic way for a function to reference itself?

There is no generic way for a function to refer to itself. Consider using a decorator instead. If all you want as you indicated was to print information about the function that can be done easily with a decorator: from functools import wraps def showinfo(f): @wraps(f) def wrapper(*args, **kwds): print(f.__name__, f.__hash__) return f(*args, **kwds) return … Read more

How can I decorate an instance method with a decorator class?

tl;dr You can fix this problem by making the Timed class a descriptor and returning a partially applied function from __get__ which applies the Test object as one of the arguments, like this class Timed(object): def __init__(self, f): self.func = f def __call__(self, *args, **kwargs): print(self) start = dt.datetime.now() ret = self.func(*args, **kwargs) time = … Read more

What is “self” used for in Swift?

Yes it is the same as this in Java and self in Objective-C, but with Swift, self is only required when you call a property or method from a closure or to differentiate property names inside your code, such as initializers. So you can use almost all of your class components safely without using self … Read more

Why isn’t self always needed in ruby / rails / activerecord?

This is because attributes/associations are actually methods(getters/setters) and not local variables. When you state “parent = value” Ruby assumes you want to assign the value to the local variable parent. Somewhere up the stack there’s a setter method “def parent=” and to call that you must use “self.parent = ” to tell ruby that you … Read more

What is the difference between class and instance variables?

When you write a class block, you create class attributes (or class variables). All the names you assign in the class block, including methods you define with def become class attributes. After a class instance is created, anything with a reference to the instance can create instance attributes on it. Inside methods, the “current” instance … Read more

How to use Ruby’s self keyword

There are several important uses, most of which are basically to disambiguate between instance methods, class methods, and variables. First, this is the best way to define class methods: class Foo def self.bar “class method bar” end def bar “instance method bar” end end Foo.bar #returns “class method bar” foo = Foo.new foo.bar #returns “instance … Read more

Is it okay to pass self to an external function

There is no problem with that whatsoever – self is an object like any other and may be used in any context where object of its type/behavior would be welcome. In Python, as exemplified by the standard library, instances of self get passed to functions (and also to methods, and even operators) all the time.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)