FAQ Database Discussion Community


Show python object name

python,django,object,self
In the django admin page, everything is just showing 'xxx object' and not the name that i write in the model: class categoria(models.Model): nomeCategoria = models.CharField(max_length=50) imagemCategoria = models.CharField(max_length=200) def __str__(self): return self.nomeCategoria class post(models.Model): tituloPost = models.CharField(max_length=200,default='tituloPost') textoPost = models.TextField() textoTagPost = models.TextField() categoria = models.ForeignKey(categoria) enviadoNewsletter = models.BooleanField(default=False)...

Self pointer, in C++ function from Lua

c++,pointers,lua,self
I'm still very new to Lua and I have a hard time trying to figure out the best approach when registering C functions in Lua, but from a C++ class, when it comes to retrieving the self pointer from an object that was not created from Lua. Consider the following...

“__weak MyClass *myWeakClass = self” using myWeakClass VS using self

ios,objective-c,self,weak-references
I am trying to learn AVFoundation sample code "AVCam". In the sample code project, AVCamViewController.m class, there is an implementation like __weak AVCamViewController *weakSelf = self; [self setRuntimeErrorHandlingObserver:[[NSNotificationCenter defaultCenter] addObserverForName:AVCaptureSessionRuntimeErrorNotification object:[self session] queue:nil usingBlock:^(NSNotification *note) { AVCamViewController *strongSelf = weakSelf; My question is why we want to first declare a...

Why am I getting a (NameError: name 'self' is not defined ) here using Kivy for Python?

python,python-2.7,kivy,self,nameerror
I'm making a game and I'm working on the screen switching. I want the screen to switch when I press the "play" button but I get a NameError instead. Here's the code: class menu_screen(Screen): def __init__(self, **kwargs): super (menu_screen,self).__init__(**kwargs) menu_layout = FloatLayout logo_label = Label(text = "GAME", size_hint=(.6, .2),pos_hint={'x':.2, 'y':.65})...

Python: I'm getting an error when using self.name

python,oop,self
I made 2 programs, one that works here: class dog: amount_of_dogs = 0 def __init__(self, name, playfulness): self.name = name self.playfulness = playfulness dog.amount_of_dogs += 1 dog1 = dog("Ruffer", 1000) dog2 = dog("Skipper", 400) dog3 = dog("El Diablo", 30000000) print "The dog names are:", dog1.name, ",", dog2.name, ",", dog3.name, "."...

Rails callback not using self

ruby-on-rails,ruby,self
Code snippet from Rails Guides : class User < ActiveRecord::Base before_validation :normalize_name, on: :create protected def normalize_name self.name = self.name.downcase.titleize end end Why did we use self.name in the right side of the statement? Can't we say self.name = name.downcase.titleize ...

Find first increasing value in vector

r,min,self,uniform
I draw a random sample from Uniform Distribution by u <- runif (1000,0,1) Now I want to calculate the value of this random variable N = min_n {n : u_n > u_{n-1}} Edit Let say I draw a random sample of size 10. So, I have u= (u_1,u_2,u_3,...,u_10). Now I...

Using threading.timer to delay sub-procedure

python-3.x,pyqt4,self
def emailCheck(self): n=0 (retcode, messages) = mail.search(None, '(UNSEEN)') if retcode == 'OK': for num in messages[0].split() : n=n+1 typ, data = mail.fetch(num,'(RFC822)') for response_part in data: if isinstance(response_part, tuple): original = email.message_from_bytes(response_part[1]) print (original['From']) print (original['Subject']) typ, data = mail.store(num,'+FLAGS','\\Seen') print (n) t = threading.Timer(10.0, emailCheck) t.start() I am trying...

Python self and super in multiple inheritance

python,inheritance,self,super,method-resolution-order
In Raymond Hettinger's talk "Super considered super speak" at PyCon 2015 he explains the advantages of using super in Python in multiple inheritance context. This is one of the examples that Raymond used during his talk: class DoughFactory(object): def get_dough(self): return 'insecticide treated wheat dough' class Pizza(DoughFactory): def order_pizza(self, *toppings):...

swift function calls: self-keyword vs without

function,swift,self
I was wondering if there is any difference between those two a function-calls in a class: self.myFuction() VS myFunction() it is working in both ways. Is there the case where it's necessary to use the self-keyword?...

Why do I need to pass an object to this class to make it work?

python,class,object,self
So I've been working to replace PHP with Python in my career for a bit. So I'm using WebPy with WSGI in apache, and all is working well, but I'm still learning the language, and can't find a simple explanation for this. So, while messing around, trying to get other...

Is it correct to use local variable name “self” in blocks?

ios,objective-c-blocks,self
I`ve found that construction __strong typeof(self)self = weakSelf. It allows remove NSAssert macro self catching, but I am in doubt is it right to use it in that way? __weak typeof(self)weakSelf = self; self.signupBlock = ^{ __strong typeof(self)self = weakSelf; NSLog (@"%d", self.property) NSAssert((self.property > 5), @"Some message"); } Pls...

Understanding self with method chaining

ruby,methods,self
I'm trying to understand self in Ruby. In the code pasted below, if I create a new instance of Animal with fox = Animal.new.name("Fox").color("red").natural_habitat("forest").specie("mammal") and then call fox.to_s It does not do anything if I do not return self in every method. Why do I need self in every method?...

Python: Self as attribute

python,methods,self
My problem is : When I test my "isProduct" method, i get the error message saying: TypeError: isProduct() takes exactly 2 arguments (1 given) So, I looked for this problem, and I found I had to add 'self' before calling my method. I did. But still, it says : NameError:...

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

python,class,self,python-decorators
Consider this small example: import datetime as dt class Timed(object): def __init__(self, f): self.func = f def __call__(self, *args, **kwargs): start = dt.datetime.now() ret = self.func(*args, **kwargs) time = dt.datetime.now() - start ret["time"] = time return ret class Test(object): def __init__(self): super(Test, self).__init__() @Timed def decorated(self, *args, **kwargs): print(self) print(args)...

Explicit use of `self` in method name in class definition

ruby,self,class-eval
This code is from the Rails Crash Course book: class Accessor def self.accessor(attr) class_eval " def #{attr} @#{attr} end def #{attr}=(val) @#{attr} = val end " end end The idea is that a subclass of Accessor can create getter and setter methods by calling the accessor method with the attribute...

PyCharm: Shadows built in name 'self', bug or feature?

python,class,pycharm,ironpython,self
I'm using PyCharm IDE; I love it for all the work it does for me. I found it very powerful in my process of learning python. It has all the hints about variables names, typos etc... However now I'm a little bit confused. When using PyCharm with IronPython 2.7, Pycharm...

What does the 'self' keyword mean in WebWorkers

javascript,web-worker,self
I don't understand the 7,8,9 line: var worker = new Worker('doWork.js'); worker.addEventListener('message', function(e) { console.log('Worker said: ', e.data); // Here it send's the data. }, false); worker.postMessage('Hello World'); // Send data to our worker. //7 self.addEventListener('message', function(e) { //8 self.postMessage(e.data); //9 }, false); What is doing this block of code?...

Use of self when creating objects in Python

python,object,self
I made several classes which are doing the same thing, but I still don't completely understand the difference, and which is best to use. Also, the 4th object is not working. It says 'NameError: name 'self' is not defined', although I don't understand what is going wrong. This is what...