-1

Please take a look at the following snippet. You can understand what I am trying to do. The vlan_start of PairTwo class must be derived from the values of PortOne. How do I do that?

>>> class G8032:
...     class PairOne:
...         vlan_start = 1101
...         vlan_count = 600
...     class PairTwo:
...         vlan_start = G8032.PairOne.vlan_start + G8032.PairOne.vlan_count
...         vlan_count = 400
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in G8032
  File "<stdin>", line 6, in PairTwo
NameError: name 'G8032' is not defined
>>> 

EDIT: I have tried without G8032 in G8032.PairOne.vlan_***. it still doesn't work.

vanangamudi
  • 673
  • 1
  • 8
  • 21
  • 2
    Simply use `PairOne.vlan_start` etc, at the time `PairTwo` is being created `G8032` doesn't exist. – Ashwini Chaudhary Jun 07 '17 at 08:45
  • 2
    `PairOne` and `PairTwo` can (should) be outside classes, or even better, `Enums`. – DeepSpace Jun 07 '17 at 08:45
  • 1
    @AshwiniChaudhary This wouldn't work – DeepSpace Jun 07 '17 at 08:45
  • 1
    With fixed attributes, you shouldn't calculate PairTwo.vlan_start: just assign it to 1701. Otherwise, make it a property, and you can obtain classes through e.g. `__class__`. –  Jun 07 '17 at 08:46
  • 2
    Why are you nesting these classes? – juanpa.arrivillaga Jun 07 '17 at 08:48
  • I need to define those classes inside G8032 class, it is important for the syntax which is used in some other file(which used this hierarchical information encoded via the python syntax - `x = G8032.PairOne.vlan_start`) in the project. Unfortunately I am not at liberty to post snippets from that file. – vanangamudi Jun 07 '17 at 08:48
  • @Evert This wouldn't work as well as long as OP keeps using nested classes. – DeepSpace Jun 07 '17 at 08:48
  • There is certainly a better pattern to do what you are doing. FactoryPattern perhaps? – cs95 Jun 07 '17 at 08:50
  • @vanangamudi This syntax doesn't imply that `PairOne` is *defined* inside `G8032` – DeepSpace Jun 07 '17 at 08:50
  • @Shiva: Can you provide a simple example, to apply factory pattern over here? – vanangamudi Jun 07 '17 at 08:51
  • @DeepSpace: I know, but I can acces the values with that syntax. – vanangamudi Jun 07 '17 at 08:52
  • 1
    Interesting, so the scope created by the nested class has no clue about its enclosing scope. Though the scope within `G8032` can still access `PairOne`. – Ashwini Chaudhary Jun 07 '17 at 08:54
  • some has down voted the question, I don't understand why. It is perfectly a valid question which has not been answere anywhere in SO to the best of my knowledge. – vanangamudi Jun 07 '17 at 08:56
  • 1
    @AshwiniChaudhary it's not that the nested class has no clue about its enclosing scope. The problem is that the enclosing scope is not completely defined yet. – DeepSpace Jun 07 '17 at 08:56
  • @DeepSpace Not really, something like `x = PairOne` within `G8032` still works even though `G8032` is still being parsed. – Ashwini Chaudhary Jun 07 '17 at 08:58

3 Answers3

2

Don't nest these classes. When the line vlan_start = G8032.PairOne.vlan_start + G8032.PairOne.vlan_count is executed, the G8032 class is not completely defined yet.

If you absolutely need to supporty the syntax you mentioned in the comments
(x = G8032.PairOne.vlan_start) then you could (but probably shouldn't) use the code below:

class PairOne:
    vlan_start = 1101
    vlan_count = 600


class PairTwo:
    vlan_start = PairOne.vlan_start + PairOne.vlan_count
    vlan_count = 40


class G8032:
    PairOne = PairOne
    PairTwo = PairTwo

print(G8032.PairOne.vlan_start)
print(G8032.PairTwo.vlan_start)
# 1101
# 1701
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
1

Why not doing something like this -

class G8032:
    pass

class PairOne:
    vlan_start = 1101
    vlan_count = 600
class PairTwo:
    p = PairOne()
    vlan_start = p.vlan_start + p.vlan_count
    vlan_count = 400


if __name__ == "__main__":
    g = G8032()
    g.PairOne = PairOne()
    g.PairTwo = PairTwo()

You may find this thread interesting - Nested classes' scope?

SRC
  • 2,123
  • 3
  • 31
  • 44
0

If you need the mentioned syntax, you could use this:

class G8032:
    PairOne = type('PairOne', (object,), dict({'vlan_start': 1101, 'vlan_count': 600}))
    PairTwo = type('PairTwo', (object,), dict({'vlan_start': PairOne.vlan_start + PairOne.vlan_count, 'vlan_count': 400 }))

print (G8032.PairOne.vlan_start)
>>> 1101
Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47