Re: Variables visibility for methods
среда, 31 августа 2016 г., 14:09:16 UTC+7 пользователь ast написал: > Hello > > I made few experiments about variables visibility > for methods. > > class MyClass: > a = 1 > def test(self): > print(a) > > obj = MyClass() > obj.test() > > Traceback (most recent call last): > File "", line 1, in > obj.test() > File "", line 4, in test > print(a) > NameError: name 'a' is not defined > > === RESTART: Shell == > > a = 1 > > class MyClass: > def test(self): > print(a) > > obj = MyClass() > obj.test() > 1 > > So it seems that when an object's méthod is executed, variables > in the scope outside the object's class can be read (2nd example), > but not variables inside the class (1st example). > > For 1st example, I know that print(MyClass.a) or print(self.a) > would have work. > > Any comments are welcome. Class construction in Python does not have scope in usual way. In fact "class" construct is essentially a syntactic sugar to call of "type" built-in function (https://docs.python.org/3.5/library/functions.html#type) and then adding all the parts manually. When you put a variable inside class definition but outside of any method body the variable becomes class attribute (so can be accessed by using MyClass.var or self.var if inside method body, keep in mind though that assigning to self.var creates instance attribute instead of changing class attribute) -- https://mail.python.org/mailman/listinfo/python-list
Issues with "twistd" in Twisted 16.4.0
Hi all I couldn't find Twisted-specific group, so posting here. Recently Twisted 16.4.0 got released. Yesterday I've tried to upgrade it for my apps and got an error. I've created simple example, which demonstrates it. File service.tac: # You can run this .tac file directly with: #twistd -ny service.tac import os from twisted.application import service, internet import mymodule application = service.Application("Demo application") File mymodule.py: def myfunction(asd): """ Stub function """ If you try to run it with twistd -y service.tac you'll get an error: >>> Failed to load application: No module named mymodule The errors comes down to this: twistd script does not add current working directory to python path, so it fails to import any packages/modules from it. It works just fine on previous version (Twisted 16.3.2). The strange thing here is that it was working before just fine, and I did not find anything related to this in release notes (http://labs.twistedmatrix.com/2016/08/twisted-1640-released.html), except that twistd now is a Setuptools console script (which I honestly don't know how can affect python path). Any ideas? -- https://mail.python.org/mailman/listinfo/python-list
Re: Issues with "twistd" in Twisted 16.4.0
Also, I've tried to find anything in Twisted bug tracker, but I can't find my way in there. -- https://mail.python.org/mailman/listinfo/python-list