Linked Questions

0 votes
0 answers
23 views

Python unittest.main() unable to instantiate inner classes [duplicate]

When I run the following test case using unittest.main(): import unittest class TestInner_Helper(unittest.TestCase): class InnerClass(object): def __init__(self): self....
quanticle's user avatar
  • 5,050
87 votes
8 answers
118k views

How to print Docstring of python function from inside the function itself?

I want to print the docstring of a python function from inside the function itself. for eg. def my_function(self): """Doc string for my function.""" # print the Docstring here. At the moment I am ...
shane87's user avatar
  • 3,130
12 votes
4 answers
6k views

Why accessing to class variable from within the class needs "self." in Python? [duplicate]

I'm learning Python and I have a question, more theoretical than practical, regarding access class variables from method of this class. For example we have: class ExampleClass: x = 123 def ...
Gill Bates's user avatar
  • 15.5k
33 votes
3 answers
13k views

Python - reference inner class from other inner class

I am trying to reference an inner class from another inner class. I have tried both : class Foo(object): class A(object): pass class B(object): other = A and class Foo(object): ...
crunk1's user avatar
  • 2,908
17 votes
1 answer
464 views

Why doesn't "class" start a new scope like "def" does?

I'm not entirely sure this is for stackoverflow, so please correct me if not. i.e. say we have t.py with contents: class A(object): pass print("A:", A) class B(object): print("From B: A:", A)...
Tim Kersten's user avatar
1 vote
3 answers
4k views

How to reference a static attribute from within a class in Python?

I have the following python snippet: class myClass: myVar = 'a' def __init__(self): self.myOtherVar = 'b' myVar = 'c' # Gets assigned but only as a local variable. ...
stratis's user avatar
  • 8,122
1 vote
2 answers
2k views

Creating objects within an existing object in Python

I would like to create an object that holds and creates different objects within itself. I have an outer class and inner classes, like this: class Outer: def __init__(self, name): self....
Didi Willems's user avatar
3 votes
3 answers
3k views

python import nested class

I'm new to python. This is not my actual case, it's just my curiosity about importing nested class. So I have a main.py and test.py. test.py: class one(): class two(): def twodef(): ...
andio's user avatar
  • 1,828
2 votes
2 answers
3k views

How to access parent class instance attribute from child class instance?

How to access the Parent's name in this example? class Parent: def __init__(self) : self.name = "toto" self.enfant = self.Child() class Child: def __init__(...
Rémi's user avatar
  • 23
4 votes
3 answers
441 views

Access base class attribute in derived class - in "class scope"

class Outer(object): class InnerBase(object): _var = {'foo', 'bar'} class Derived(InnerBase): _var = _var | {'baz'} # NameError: name '_var' is not defined _var = InnerBase....
Mr_and_Mrs_D's user avatar
  • 34.5k
-1 votes
1 answer
1k views

nested classes - how to use function from parent class?

If I have this situation: class Foo(object): def __init__(self): self.bar = Bar() def do_something(self): print 'doing something' class Bar(object): def __init(...
Udi Goldberg's user avatar
0 votes
1 answer
916 views

Python class hierarchy with nested layout - does it make sense?

I ran into some strange errors and just ask myself if I have a fundamental issue in my class hierarchy. I am an absolute newbie if it comes to python. So if this approach appears llike total crap - ...
arkascha's user avatar
  • 43.2k
-1 votes
1 answer
653 views

Use instance attributes of outer class instances in inner class

I'm trying to append inner class instances into a list which is defined as an attribute of the outer class instances. However I get an "AttributeError: type object 'Outer' has no attribute 'Inner_list'...
Franz Eskö's user avatar
0 votes
0 answers
321 views

Call parent instance's method inside of child instance

What's the best way of having a child class (B) instance call one of its parent class (A) instance's methods? Passing self into the instance of the child class (B) seems to work, but somehow that ...
SkyNT's user avatar
  • 803
2 votes
1 answer
191 views

Override list's builtins dynamically in class scope

Purely curiosity question: class Li(list): pass m, n= Li([1]), Li([2]) def r(*args, **kwargs): raise Exception('hop') setattr(m, '__iadd__', r) m += n print m # [1, 2] setattr(Li, '__iadd__', r) m += ...
Mr_and_Mrs_D's user avatar
  • 34.5k

15 30 50 per page