How does asigning of @property works? (Assigning of a getter to another variable.)

I have code like this:

class Debug(object): ... @property def ThreadAwareLogger(self): if not self.logger_instance_for_current_thread: self.logger_instance_for_current_thread=self.cloneMainLogger() return self.logger_instance_for_current_thread def cloneMainLogger(self): return logger_instance_for_current_thread class SomeOtherClass(object): def __init__(self): ... self.logger=debug.ThreadAwareLogger def otherMethod(self): ..."Message")

The problematic thing is the assignment self.logger=debug.ThreadAwareLogger. I'm not sure what is gonna be content of self.logger. I want it to be the whole getter and I want to have the getter executed everytime when I use self.logger in SomeOtherClass. But I'm affraid that in self.logger will be stored just the result of the getter logger_instance_for_current_thread. That means logger of the thread which was active at the time of assignment. This logger doesn't have to be the right one when I call"Message").

How to get the getter ThreadAwareLogger executed everytime when I call"Message")?

Sidenote: Why I actually need the shortcut self.logger? When I decide to replace ThreadAwareLogger with AdvancedThreadAwareLogger I will change just one assignment instead of thousand calls of"Message"). Also is None of the business of the otherMethod to care about which logger will be used.


The assignment of getter works as described in the answer by @unutbu. But assigning of the getter causes an issue I didn't think before.

From the ThreadAwareLogger I actually call method cloneMainLogger. Now the call of someOtherClass.logger ends with exception: AttributeError: 'SomeOtherClass' object has no attribute 'cloneMainLogger'.

So far I bypassed the issue with a small hack. The SomeOtherClass actually has a Debug instance. So I call debug.cloneMainLogger() instead of self.cloneMainLogger() inside of ThreadAwareLogger. The program now works, but I consider it really dirty.

Edit 2:

If I add line self.comptreeLogger=Debug.ThreaAwareLogger inside of a method; e.g. cloneMainLogger I'm getting AttributeError: 'property' object has no attribute 'debug'. Conclusion: I still don't understand the solution.

-------------Problems Reply------------

import logging
import random

class Debug(object):
def ThreadAwareLogger(self):
loggers = [logging.getLogger('abc'),logging.getLogger('def')]
return self.anotherDebugMethod(loggers)
def anotherDebugMethod(self,loggers):
return random.choice(loggers)
class SomeOtherClass(object):
def __init__(self):
def logger(self):
return self.debug.ThreadAwareLogger
def otherMethod(self):'Message')

if __name__=='__main__':
for i in range(5):

yields output like:


Notice the changes in abc and def show that the Debug.ThreadAwareLogger function is getting called each time.

One way to get the whole property would be to just make another property:

class SomeOtherClass:

def logger(self):
return self.debug.ThreadAwareLogger

There might be other ways, too.

self.logger will refer to what is returned by the property ThreadAwareLogger, not to the property itself.

Category:python Views:0 Time:2011-09-05

Related post

  • what is diff. b/w @property (nonatomic,assign) and @property (nonatomic,retain) 2011-03-18

    Possible Duplicate: Are @property(nonatomic)ivar @property(nonatomic,assign)ivar the same or different? i have seen in many classes that uses delegate which use this keyword assign instead of retain. y we have use assign instead retain. may i use ass

  • Are @property(nonatomic)ivar @property(nonatomic,assign)ivar the same or different? 2011-03-18

    Are @property(nonatomic)ivar and @property(nonatomic,assign)ivar the same or different? --------------Solutions------------- As stated in the docs: assign - Specifies that the setter uses simple assignment. This attribute is the default. So no, as fa

  • @property (nonatomic, assign) id delegate; 2011-09-11

    Anyone can explain me what this line mean ? I use to see (nonatomic,retain) it's first time I see the "assign" keyword in: @property (nonatomic, assign) id <IconDownloaderDelegate> delegate; Thx for your help, Stephane --------------Solutions--

  • Should I assign my DTO properties to public variables or call the dto instance directly on my aspx page? 2010-11-03

    My UI layer calls my business layer which populates a DTO. I need to display properties from the DTO on my ASPX page. Should I create public variables on the code behind page for each of the DTO properties and reference like <%=PublicPropertyName%

  • Is it bad coding style to not assign a new instance to a variable? 2011-06-03

    At the top of my program I have a code segment that looks like this var XXXAssembler = new XXXAssembler(ctx); XXXAssembler.LoadXXX(); var YYYAssembler = new YYYAssembler(ctx ); YYYAssembler.LoadYYY(); var ZZZAssembler = new ZZZAssembler(ctx); ZZZAsse

  • How to assign a heredoc value to a variable in Bash? 2009-07-22

    I have this multi-line string (quotes included) abc'asdf" $(dont-execute-this) foo"bar"'' How would I assign it to a variable using a heredoc in Bash? I need to preserves newlines. I don't want to escape the characters in the string, that would be an

  • Weird PHP behavior: won't assign the integer 8 to a variable 2009-08-25

    I think I just encountered the strangest 'bug' I've ever encountered in my short developer life. It seems like I just can't assign the value eight to any variable. For exemple: <?php $seven = 07; $eight = 08; //what's wrong here? $sevenB = 7; $eig

  • Assigning an array element to a variable with the same name? 2009-11-11

    I'd like to be able to extract some array elements, assign each of them to a variable and then unset these elements in the array. Let's say I have $myarray = array ( "one" => "eins", "two" => "zwei" , "three" => "drei") ; I want a function s

  • how do I assign page referrer to a php variable 2010-03-03

    I am using javascript to get the referring page (document.referrer) which works fine and I can add this value to a div no problem. How do I assign this value to a PHP variable in order to do some other things with it? Ideally, something like: $myphpv

  • Objective-C: Assigning the value of a static variable to an instance variable 2010-07-12

    I essentially want to give each instance of a class a unique id. So, I created a static integer. I increment it each time a new object is created and then assign the value of the static variable to an ivar. But clearly I don't understand something be

  • Java - assigning two expressions to a single variable simultaneously 2010-10-22

    I've just finished my first year of Java and I've been looking at the JDK source code as an exercise. I came across something I've never encountered before where there were two assignments to the same variable in the same statement. e.g. variable = e

  • jQuery - Assign a html code to a variable 2010-12-03

    Suppose I have a code like that <p>hello</p> I want to assign the above code to a variable, how to do that? --------------Solutions------------- var content = $('p').html(); Ah dammit, too late. If you want a specific paragraph you should

  • To assign the value of a JavaScript variable into Java variable 2011-03-01

    I have encountered a situation where I have to assign the value of a JavaScript variable into Java variable. Please suggest me a way to do it. P.S: Assigning value of Java variable into JavaScript is something like this: < Script>< % String

  • Assign values to a group of variables using recursion in C#? 2011-03-10

    How can I assign values to a group of variables using recursion in C#? I can easily do it with a simple procedure, but I have no idea how to do it using recursion. public void Assign() { Console.Write("Name: "); Name = Console.ReadLine(); Console.Wri

  • How to intercept assigning new value for the In variable? 2011-04-11

    I wish to intercept assigning new values for the In variable. I have tried to do this by defining UpValues for In but it does not help in this case: Unprotect[In]; In /: Set[In[line_], expr_] /; ! TrueQ[$InsideSet] := Block[{$InsideSet = True}, Print

  • How to assign a memory address to a variable in Python? 2011-04-29

    here's the scenario: I foolishly forget to assign the returned object to a variable: >>> open("random_file.txt") <open file 'random_file.txt', mode 'r' at 0x158f780> Is there a way to directly assign the memory address to a variable? S

  • Reading line by line from text file and assign each value into a specific variable 2011-05-13

    I have to build an automated test scenario where I need to populate multiple fields in a web page. The data to populate the specific fields come from a text file in1=data1,in2=data2,in3=data3 in11=data11,in12=data12,in13=data13 in21=data21,in22=data2

  • why can't I assign 1.2 value to float variable in C#? I did the same in C and C++, and it worked 2011-07-17

    why can't I assign 1.2 value to float variable in C#? I did the same in C and C++, and it worked. class Program { private readonly int a = 20; private readonly float b; public Program(float tmp) { b = tmp; } static void Main(string[] args) { Program

  • Can I assign a static class to a variable? 2011-07-22

    My question is whether I can assign a static class to another variable (and of what type would that be) ? Say I have public static class AClassWithALongNameIDontWantTotType{ public static bool methodA() { stuff } } and then I have class B{ } Can I ma

  • Why must we assign a clone to a new variable? 2011-10-02

    I am currently learning to use the Propel ORM, and I want to reuse a critera for two slightly different queries: $criteria = ArticleQuery::create() ->filterByIsPublished(true) ->orderByPublishFrom(Criteria::DESC) ->joinWith('Article.Author')

Copyright (C), All Rights Reserved.

processed in 0.133 (s). 11 q(s)